有时候不用hibernate,用jdbc如何与spring结合呢?,一篇简单的文章,适合像我这样的菜鸟学习。
一个简单的实例,目录截图
首先实体和接口
package com.entity;
public class Person {
private int id;
private String name;
public Person(String name) {
this.name = name;
}
public Person(int id, String name) {
this.id = id;
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 com.service;
import java.util.List;
import com.entity.Person;
public interface PersonService {
/**
* 添加
*
* @param person
*/
public void save(Person person);
/**
* 修改
*
* @param person
*/
public void update(Person person);
/**
* 删除
*
* @param id
*/
public void delete(int id);
/**
* 查询单个
*
* @param id
* @return
*/
public Person getPerson(int id);
/**
* 查询所有的
*
* @return
*/
public List<Person> getPersons();
}
业务实现类
package com.serviceImpl;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import com.entity.Person;
import com.service.PersonService;
public class PersonServiceImpl implements PersonService {
private JdbcTemplate jt;
public void setDataSource(DataSource dataSource) {
this.jt = new JdbcTemplate(dataSource);
}
@Override
public void delete(int id) {
jt.update("delete from person where id=?", new Object[]{id});
}
@Override
public Person getPerson(int id) {
return (Person)jt.queryForObject("select * from person where id="+id, new PersonRowMapper());
}
@SuppressWarnings("unchecked")
@Override
public List<Person> getPersons() {
return (List<Person>)jt.query("select * from person", new PersonRowMapper());
}
@Override
public void save(Person person) {
jt.update("insert into person(name) values(?)", new String[] { person
.getName() });
}
@Override
public void update(Person person) {
jt.update("update person set name=? where id=?", new Object[] {
person.getName(),person.getId() });
}
}
在查询时需要一 个实现RowMapper接口的类。
package com.serviceImpl;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
import com.entity.Person;
public class PersonRowMapper implements RowMapper {
@Override
public Object mapRow(ResultSet r, int index) throws SQLException {
Person person = new Person(r.getInt("id"),r.getString("name"));
return person;
}
}
xml的配置信息大学一看就明白
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/person?characterEncoding=UTF-8">
</property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<!-- 连接沲初始时启动值 -->
<property name="initialSize" value="1"></property>
<!-- 连接沲的最大值 -->
<property name="maxActive" value="100"></property>
<!-- 过高峰期最大连接值 -->
<property name="maxIdle" value="2"></property>
<!-- 最小空闲值 -->
<property name="minIdle" value="1"></property>
</bean>
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<aop:config>
<aop:pointcut id="bussinessService"
expression="execution (* com.serviceImpl.PersonServiceImpl.*(..))" />
<aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<bean id="personService" class="com.serviceImpl.PersonServiceImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
测试类运行下
package com.test;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.entity.Person;
import com.service.PersonService;
public class SpringJdbcTest {
private static PersonService ps;
@BeforeClass
public static void setUpBeforeClass() throws Exception{
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
ps = (PersonService) ac.getBean("personService");
}
@Test
public void save() {
ps.save(new Person("mdc"));
}
@Test
public void update() {
ps.update(new Person(1,"www"));
}
@Test
public void delete() {
ps.delete(1);
}
@Test
public void getPerson() {
Person person = ps.getPerson(2);
System.out.println(person.getId()+"===="+person.getName());
}
@Test
public void getPersons() {
List<Person> persons = ps.getPersons();
for(Person person:persons){
System.out.println(person.getId()+"===="+person.getName());
}
}
}