Java开发之Spring连接数据库方法实例分析

网友投稿 199 2023-07-27


Java开发之Spring连接数据库方法实例分析

本文实例讲述了java开发之Spring连接数据库方法。分享给大家供大家参考,具体如下:

接口:

package cn.com.service;

import java.util.List;

import cn.com.bean.PersonBean;

public interface PersonService {

//保存

public void save(PersonBean person);

//更新

public void update(PersonBean person);

//获取person

public PersonBean getPerson(int id);

public List getPersonBean();

//删除记录

public void delete(int personid);

}

Person Bean类:

package cn.com.bean;

public class PersonBean {

private int id;

private String name;

public PersonBean(String name) {

this.name=name;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

接口实现:

package cn.com.service.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import cn.com.bean.PersonBean;

import cn.com.service.PersonService;

public class PersonServiceImpl implements PersonService {

private JdbcTemplate jdbcTemplate;

public void setDataSource(DataSource dataSource) {

this.jdbcTemplate = new JdbcTemplate(dataSource);

}

@Override

public void save(PersonBean person) {

// TODO Auto-generated method stub

jdbcTemplate.update("insert into person(name) values(?)", new Object[]{person.getName()},

new int[]{java.sql.Types.VARCHAR});

}

@Override

public void update(PersonBean person) {

// TODO Auto-generated method stub

jdbcTemplate.update("update person set name=? where id=?", new Object[]{person.getName(),person.getId()},

new int[]{java.sql.Types.VARCHAR,java.sql.Types.INTEGER});

}

@Override

public PersonBean getPerson(int id) {

// TODO Auto-generated method stub

return (PersonBean)jdbcTemplate.queryForObject("select * from person where id=?", new Object[]{id},

new int[]{java.sql.Types.INTEGER},new PersonRowMapper() );

}

@SuppressWarnings("unchecked")

@Override

public List getPersonBean() {

// TODO Auto-generated method stub

return (List)jdbcTemplate.query("select * from person",

new PersonRowMapper() );

}

@Override

public void delete(int personid) {

// TODO Auto-generated method stub

jdbcTemplate.update("delete from person where id=?", new Object[]{personid},

new int[]{java.sql.Types.INTEGER});

}

}

RowMapper:

package cn.com.service.impl;

import java.sql.ResultSet;

import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import cn.com.bean.PersonBean;

public class PersonRowMapper implements RowMapper {

@Override

public Object mapRow(ResultSet rs, int index) throws SQLException {

// TODO Auto-generated method stub

PersonBean person =new PersonBean(rs.getString("name"));

person.setId(rs.getInt("id"));

return person;

}

}

beans.xml配置

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-2.5.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-2.5.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-2.5.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-2.5.xsd

">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-2.5.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-2.5.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-2.5.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-2.5.xsd

">

测试类:

package Junit.test;

import static org.junit.Assert.*;

import org.junit.BeforeClass;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.com.bean.PersonBean;

import cn.com.service.PersonService;

public class PersonTest2 {

private static PersonService personService;

@BeforeClass

public static void setUpBeforeClass() throws Exception {

ApplicationContext act=new ClassPathXmlApplicationContext("beans.xml");

personService=(PersonService) act.getBean("personService");

}

@Test

public void save() {

personService.save(new PersonBean("wyy"));

}

@Test

public void update() {

PersonBean person=personService.getPerson(1);

person.setName("wy");

personService.update(person);

}

@Test

public void getPerson() {

PersonBean person=personService.getPerson(1);

System.out.println(person.getName());

}

@Test

public void delete() {

personService.delete(1);

}

}

数据库:

Create Table

CREATE TABLE `person` (

`id` int(11) NOT NULL auto_increment,

`name` varchar(10) NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8

希望本文所述对大家Java程序设计有所帮助。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:用Java集合中的Collections.sort方法如何对list排序(两种方法)
下一篇:JDBC编程实现文件、图片的存储方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~