创建数据库表
CREATE TABLE `test_user` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`testName` varchar(20) DEFAULT NULL,
`age` bigint(20) DEFAULT NULL,
`sex` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 COMMENT='测试用户表';
创建实体:
package com.spring.jdbc.model; public class User { private int id; private String testName; private String sex; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTestName() { return testName; } public void setTestName(String testName) { this.testName = testName; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
创建Mapper类:
package com.spring.jdbc.mapper; import java.sql.ResultSet; import java.sql.SQLException; import org.springframework.jdbc.core.RowMapper; import com.spring.jdbc.model.User; public class UserRowMapper implements RowMapper { @Override public Object mapRow(ResultSet rs, int rowNum) throws SQLException { User person = new User(); person.setId(rs.getInt("id")); person.setAge(rs.getInt("age")); person.setTestName(rs.getString("testName")); person.setSex(rs.getString("sex")); return person; } }
创建service接口:
package com.spring.jdbc.service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.spring.jdbc.model.User; @Transactional(propagation = Propagation.REQUIRED) public interface UserService { public void save(User user) throws Exception; }
创建service接口实现类:
package com.spring.jdbc.service.impl; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; import com.spring.jdbc.model.User; import com.spring.jdbc.service.UserService; public class UserServiceImpl implements UserService { private JdbcTemplate template; public void setDataSource(DataSource dataSource){ this.template = new JdbcTemplate(dataSource); } @Override public void save(User user) throws Exception { template.update("insert into test_user(testName,age,sex) values(?,?,?)", new Object[]{user.getTestName(),user.getAge(),user.getSex()},new int[]{java.sql.Types.VARCHAR,java.sql.Types.INTEGER,java.sql.Types.VARCHAR} ); throw new Exception(); } }
编写配置文件:
<?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:context="http://www.springframework.org/schema/context" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <!-- http://www.cnblogs.com/adolfmc/archive/2013/01/22/2872298.html --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/spring"/> <property name="username" value="root" /> <property name="password" value="root" /> <property name="initialSize" value="1" /> <property name="minIdle" value="1" /> <property name="maxActive" value="300" /> <property name="maxIdle" value="2" /> </bean> <bean id="userService" class="com.spring.jdbc.service.impl.UserServiceImpl"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name="dataSource" ref="dataSource" /> </bean> <!-- 开启事务注解驱动 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
编写测试类:
package com.spring.jdbc; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.jdbc.model.User; import com.spring.jdbc.service.UserService; public class SpringJdbcTest { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-jdbc.xml");//BeanDefination的解析注册,代理对象的生成 UserService userService = (UserService)applicationContext.getBean("userService"); User user = new User(); user.setAge(24); user.setTestName("测试"); user.setSex("man"); userService.save(user); } }
运行结果: