一、Spring的jdbcTemplate操作
1.导入所需jar包
2.设置数据库信息
3.创建jdbcTemplate对象
4.调用方法
- 添加操作
@Test
public void add(){
//设置数据库信息
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/spring?serverTimezone=UTC");
dataSource.setUsername("root");
dataSource.setPassword("123");
//创建jdbcTemplate对象,设置数据源
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
//调用方法实现
//编写sql语句
String sql = "insert into user values(?,?)";
jdbcTemplate.update(sql, "baozi", "250");
}
- 修改操作
@Test
public void update(){
//将之前的数据库信息设置封装成了一个方法
DriverManagerDataSource dataSource = JdbcUtils.getDataSource();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "update user set password = ? where username = ?";
jdbcTemplate.update(sql, "sb" , "baozi");
}
- 删除操作
@Test
public void delete(){
DriverManagerDataSource dataSource = JdbcUtils.getDataSource();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "delete from user where username = ?";
jdbcTemplate.update(sql, "baozi");
}
- 查询操作
(1)查询返回某一个值
@Test
public void select1(){
DriverManagerDataSource dataSource = JdbcUtils.getDataSource();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "select count(*) from user";
//返回一个值用queryForObject方法,第一个参数为sql语句,第二个参数为返回值类型的class
int count = jdbcTemplate.queryForObject(sql, Integer.class);
System.out.println(count);
}
(2)查询返回对象
queryForObject方法,第一个参数为sql语句,第二个参数为RowMapper接口,需要自己写类实现接口,第三个参数是查询参数
class MyRowMapper implements RowMapper<User>{
@Override
public User mapRow(ResultSet rs, int num) throws SQLException {
//1.从结果集中得到数据
String username = rs.getString("username");
String password = rs.getString("password");
//2.把得到的数据封装到对象中
User user = new User();
user.setUsername(username);
user.setPassword(password);
return user;
}
}
@Test
public void select2(){
DriverManagerDataSource dataSource = JdbcUtils.getDataSource();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "select * from user where username = ?";
User user = jdbcTemplate.queryForObject(sql, new MyRowMapper(), "孙笑川");
System.out.println(user);
}
(3)返回list集合
@Test
public void select3(){
DriverManagerDataSource dataSource = JdbcUtils.getDataSource();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "select * from user";
List<User> list = jdbcTemplate.query(sql, new MyRowMapper());
System.out.println(list);
}
二、Spring配置连接池
1.导入jar包
2.配置连接池
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring?serverTimezone=UTC"></property>
<property name="user" value="root"></property>
<property name="password" value="123"></property>
</bean>
三、在dao中使用jdbcTemplate
1.创建service对象,在service中注入dao
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(){
userDao.add();
}
}
2.在dao对象中注入jdbcTemplate
public class UserDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void add(){
String sql = "insert into user values(?,?)";
jdbcTemplate.update(sql, "嗯扫", "eight");
}
}
3.在jdbcTemplate中注入dataSource
<bean id="userDao" class="cn.itcast.c3p0.UserDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="userService" class="cn.itcast.c3p0.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 把dataSource注入到模板对象中 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring?serverTimezone=UTC"></property>
<property name="user" value="root"></property>
<property name="password" value="123"></property>
</bean>
测试代码
@Test
public void testC3p0(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
四、Spring事务管理
1.声明式事务管理(xml)
<!-- 1.配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 2.配置事务增强 -->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<!-- 做事务操作 -->
<tx:attributes>
<!-- 设置进行事务操作的方法匹配规则,name属性为做事务操作的方法名 -->
<tx:method name="account*"/>
</tx:attributes>
</tx:advice>
<!-- 3.配置切面 -->
<aop:config>
<!-- 切入点 -->
<aop:pointcut expression="execution()" id="pointcut1"/>
<!-- 切面 -->
<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/>
</aop:config>
2.声明式事务管理(注解)
<!-- 1.配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 2.开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 3.在需要做事务操作的方法所在的类上使用注解@Transactional