applicationContext.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:context=“http://www.springframework.org/schema/context”
xmlns:aop=“http://www.springframework.org/schema/aop”
xmlns:tx="http://
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<context:property-placeholder location=“classpath:db.properties” />
<context:component-scan base-package=“com.xbj.dao.impl” />
<tx:advice transaction-manager=“transactionManager” id=“transactionInterceptor”>
tx:attributes
<tx:method name=“add*” />
<tx:method name=“insert*”/>
<tx:method name=“save*”/>
<tx:method name=“update*”/>
<tx:method name=“change*” />
<tx:method name=“del*”/>
<tx:method name=“remove*”/>
<tx:method name=“drop*”/>
</tx:attributes>
</tx:advice>
aop:config
<aop:advisor advice-ref=“transactionInterceptor” pointcut=“execution(* com.xbj.service.impl..(…))” />
</aop:config>
db.properties
jdbc.url=jdbc:mysql://localhost:3306/db1
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=root
UserDAO.java
package com.xbj.dao;
import com.xbj.po.User;
import java.util.List;
public interface UserDAO {
void addUser(User user);
void delUser(Integer id);
void updateUser(User user);
User findUserById(Integer id);
List findAll();
User findUserById2(Integer id);
}
UserDAOImpl.java
package com.xbj.dao.impl;
import com.xbj.dao.UserDAO;
import com.xbj.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Repository
public class UserDAOImpl extends JdbcDaoSupport implements UserDAO {
@Resource
public void init(DataSource dataSource){
super.setDataSource(dataSource);
}
public void addUser(User user) {
//String sql, @Nullable Object… args
String sql = “INSERT INTO t_user VALUES (NULL,?,?,?)”;
this.getJdbcTemplate().update(sql, user.getUsername(), user.getPassword(), user.getAge());
}
public void delUser(Integer id) {
String sql = “DELETE FROM t_user WHERE id=?”;
this.getJdbcTemplate().update(sql, id);
}
public void updateUser(User user) {
String sql = “UPDATE t_user SET username=? ,password=?,age=? WHERE id=?”;
this.getJdbcTemplate().update(sql, user.getUsername(), user.getPassword(), user.getAge(), user.getId());
}
//自动映射,但要保证User类中的字段名与数据库的要一致
public User findUserById(Integer id) {
String sql = “SELECT * FROM t_user WHERE id=?”;
List list = this.getJdbcTemplate().query(sql, new BeanPropertyRowMapper(User.class), id);
return list.size() > 0 ? list.get(0) : null;
}
public List findAll() {
String sql = “SELECT * FROM t_user”;
return this.getJdbcTemplate().query(sql, new BeanPropertyRowMapper(User.class));
}
//手动映射
public User findUserById2(Integer id) {
String sql = “SELECT * FROM t_user WHERE id=?”;
return this.getJdbcTemplate().query(sql, new ResultSetExtractor() {
public User extractData(ResultSet rs) throws SQLException, DataAccessException {
User user=new User();
if(rs.next()){
user.setId(rs.getInt(“id”));
user.setUsername(rs.getString(“username”));
user.setPassword(rs.getString(“pwd”));
user.setAge(rs.getInt(“age”));
}
return user;
}
}, id);
}
}
原生测试方法:
package com.xbj;
import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Test01 {
@Test
public void t1() throws Exception {
Class.forName(“com.mysql.jdbc.Driver”);
//DriverManager JDBC大管家
Connection connection=DriverManager.getConnection(“jdbc:mysql://localhost:3306/db1”,“root”,“root”);
PreparedStatement psmt=connection.prepareStatement(“SELECT * from t_user WHERE age > ?”);
psmt.setInt(1,33);
ResultSet rs=psmt.executeQuery();
while (rs.next()){
Integer id= rs.getInt(“id”);
String username=rs.getString(“username”);
System.out.println(id+"–"+username);
}
}
}
Spring测试方法;
package com.xbj;
import com.xbj.dao.UserDAO;
import com.xbj.po.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {“classpath:applicationContext.xml”})
public class Test02 {
@Autowired
private UserDAO userDAO;
@Test
public void t1(){
List users=userDAO.findAll();
for (User user:users){
System.out.println(user);
}
}
@Test
public void t2(){
User user=userDAO.findUserById(8);
System.out.println(user);
}
@Test
public void t3(){
User user=new User();
user.setUsername(“hanmeimei”);
user.setPassword(“ok”);
user.setAge(16);
userDAO.addUser(user);
}