第九章 Spring5 高级应用及核心原理(三)

2024-03-25 04:04

本文主要是介绍第九章 Spring5 高级应用及核心原理(三),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、事务管理

1.1 JdbcTemplate
- 添加依赖
  <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.gupaoedu</groupId><artifactId>SpringJdbcTemplate</artifactId><version>1.0-SNAPSHOT</version><dependencies><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>5.1.17.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.1.17.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><!-- 如果Mysql是8.0的就用这个驱动 --><!--<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.16</version></dependency>--><!-- 如果Mysql是5.7的就用这个驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.38</version></dependency><!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.1</version></dependency></dependencies></project>
  
- 创建 User
  package com.gupaoedu.pojo;public class User {private Integer id;private String name;private Integer age;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", age=" + age +'}';}}
  
- 新建 IUserDao
  package com.gupaoedu.dao;import com.gupaoedu.pojo.User;import java.util.List;public interface IUserDao {public Integer addUser(User user);public List<User> query();}
  
- 新建 UserDaoImpl
  package com.gupaoedu.dao.impl;import com.gupaoedu.dao.IUserDao;import com.gupaoedu.pojo.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.RowMapper;import org.springframework.stereotype.Repository;import java.sql.ResultSet;import java.sql.SQLException;import java.util.List;@Repositorypublic class UserDaoImpl implements IUserDao {@Autowiredprivate JdbcTemplate template;private String sql;public Integer addUser(User user) {sql = "insert into users(name, age) values(?, ?)";return template.update(sql, user.getName(), user.getAge());}public List<User> query() {sql = "select * from users order by id desc";List<User> users = template.query(sql, new RowMapper<User>() {public User mapRow(ResultSet resultSet, int i) throws SQLException {User user = new User();try {user.setId(resultSet.getInt("id"));user.setName(resultSet.getString("name"));user.setAge(resultSet.getInt("age"));} catch (SQLException e) {e.printStackTrace();}return user;}});return users;}}
  
- 新建 IUserService
  package com.gupaoedu.service;import com.gupaoedu.pojo.User;import java.util.List;public interface IUserService {public Integer addUser(User user);public List<User> query();}
  
- 新建 UserServiceImpl
  package com.gupaoedu.service.impl;import com.gupaoedu.dao.IUserDao;import com.gupaoedu.pojo.User;import com.gupaoedu.service.IUserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class UserServiceImpl implements IUserService {@Autowiredprivate IUserDao dao;public Integer addUser(User user) {return dao.addUser(user);}public List<User> query() {return dao.query();}}
  
- 新建 JavaConfig
  package com.gupaoedu;import com.gupaoedu.pojo.User;import com.gupaoedu.service.IUserService;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.*;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.datasource.DriverManagerDataSource;import javax.sql.DataSource;@Configuration@ComponentScanpublic class JavaConfig {@Beanpublic DataSource dataSource() {DriverManagerDataSource dataSource = new DriverManagerDataSource();dataSource.setDriverClassName("com.mysql.jdbc.Driver");dataSource.setUrl("jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8");dataSource.setUsername("root");dataSource.setPassword("123456");return dataSource;}@Bean@DependsOn("dataSource")public JdbcTemplate template(DataSource dataSource) {return new JdbcTemplate(dataSource);}public static void main(String[] args) {ApplicationContext ac = new AnnotationConfigApplicationContext(JavaConfig.class);IUserService bean = ac.getBean(IUserService.class);User user = new User();user.setName("灰灰");user.setAge(18);System.out.println(bean.addUser(user));System.out.println(bean.query());}}
  
- 执行结果
1
[User{id=1, name='灰灰', age=18}]
 
1.2 jdbc 事务管理机制
- 【IUserDao】新增 addUserInfo 和 transaction 接口;
  package com.gupao.dao;import com.gupao.pojo.User;import java.util.List;public interface IUserDao {public Integer addUser(User user);public List<User> query();public Integer addUserInfo(String username, String password);public Integer transaction(User user, String username, String password);}
 
- 【UserDaoImpl】实现 addUserInfo 和 transaction 接口;
  package com.gupao.dao.impl;import com.gupao.dao.IUserDao;import com.gupao.pojo.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.RowMapper;import org.springframework.stereotype.Repository;import java.sql.*;import java.util.List;@Repositorypublic class UserDaoImpl implements IUserDao {private final String DRIVERNAME = "com.mysql.jdbc.Driver";private final String URL = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8";private final String USERNAME = "root";private final String PASSWORD = "123456";@Autowiredprivate JdbcTemplate template;private String sql;private Connection conn = null;private PreparedStatement ps = null;public Integer addUser(User user) {/*sql = "insert into users(name, age) values(?, ?)";int iRet = template.update(sql, user.getName(), user.getAge());*/int iRet = 0;try {Class.forName(DRIVERNAME);conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);sql = "insert into users(name, age) values(?, ?)";ps = conn.prepareStatement(sql);ps.setString(1, user.getName());ps.setInt(2, user.getAge());iRet = ps.executeUpdate();} catch (Exception e) {e.printStackTrace();} finally {if(ps != null) {try {ps.close();} catch (SQLException e) {e.printStackTrace();}}if(conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}return iRet;}public Integer addUserInfo(String userName, String passwrod) {/*sql = "insert into user_info(username, password) values(?, ?)";int iRet = template.update(sql, userName, passwrod);*/int iRet = 0;try {Class.forName(DRIVERNAME);conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);sql = "insert into user_info(username, password) values(?, ?)";ps = conn.prepareStatement(sql);ps.setString(1, userName);ps.setString(2, passwrod);iRet = ps.executeUpdate();} catch (Exception e) {e.printStackTrace();} finally {if(ps != null) {try {ps.close();} catch (SQLException e) {e.printStackTrace();}}if(conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}return iRet;}public Integer transaction(User user, String username, String password) {int iRet = 0;try {Class.forName(DRIVERNAME);conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);// 关闭自动提交conn.setAutoCommit(false);sql = "insert into users(name, age) values(?, ?)";ps = conn.prepareStatement(sql);ps.setString(1, user.getName());ps.setInt(2, user.getAge());iRet = ps.executeUpdate();sql = "insert into user_info(username, password1) values(?, ?)";ps = conn.prepareStatement(sql);ps.setString(1, username);ps.setString(2, password);iRet += ps.executeUpdate();conn.commit();} catch (Exception e) {try {conn.rollback();} catch (SQLException e1) {e1.printStackTrace();}e.printStackTrace();} finally {if(ps != null) {try {ps.close();} catch (SQLException e) {e.printStackTrace();}}if(conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}return iRet;}public List<User> query() {sql = "select * from users order by id desc";List<User> list = template.query(sql, new RowMapper<User>() {public User mapRow(ResultSet resultSet, int i) throws SQLException {User user = new User();try {user.setId(resultSet.getInt("id"));user.setName(resultSet.getString("name"));user.setAge(resultSet.getInt("age"));} catch (SQLException e) {e.printStackTrace();}return user;}});return list;}}
  
- 【IUserService】新增 addUserInfo 和 transaction 接口;
  package com.gupao.service;import com.gupao.pojo.User;import java.util.List;public interface IUserService {public Integer addUser(User user);public List<User> query();public Integer addUserInfo(String username, String password);public Integer transaction(User user, String username, String password);}
  
- 【UserServiceImpl】新增 addUserInfo 和 transaction;
  package com.gupao.service.impl;import com.gupao.dao.IUserDao;import com.gupao.pojo.User;import com.gupao.service.IUserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class UserServicImpl implements IUserService {@Autowiredprivate IUserDao userDao;public Integer addUser(User user) {return userDao.addUser(user);}public List<User> query() {return userDao.query();}public Integer addUserInfo(String username, String password) {return userDao.addUserInfo(username, password);}public Integer transaction(User user, String username, String password) {return userDao.transaction(user, username, password);}}
  
- 修改 JavaConfig
  package com.gupao;import com.gupao.pojo.User;import com.gupao.service.IUserService;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.*;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.datasource.DriverManagerDataSource;import javax.sql.DataSource;@Configuration@ComponentScanpublic class JavaConfig {@Beanpublic DataSource dataSource() {DriverManagerDataSource dataSource = new DriverManagerDataSource();dataSource.setDriverClassName("com.mysql.jdbc.Driver");dataSource.setUrl("jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8");dataSource.setUsername("root");dataSource.setPassword("123456");return dataSource;}@Bean@DependsOn("dataSource")public JdbcTemplate template(DataSource dataSource) {return new JdbcTemplate(dataSource);}public static void main(String[] args) {ApplicationContext ac = new AnnotationConfigApplicationContext(JavaConfig.class);IUserService bean = ac.getBean(IUserService.class);User user = new User();user.setName("灰灰");user.setAge(18);/*System.out.println(bean.addUser(user));System.out.println(bean.addUserInfo("huihui", "123456"));*/System.out.println(bean.transaction(user, "huihui", "123456"));}}
  
1.3 jdk 代理机制实现事务管理
- 新建【DbUtils】
  package com.gupao.utils;import java.sql.Connection;import java.sql.DriverManager;public class DbUtils {private static final String DRIVERNAME = "com.mysql.jdbc.Driver";private static final String URL = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8";private static final String USERNAME = "root";private static final String PASSWORD = "123456";private static Connection conn = null;public static  Connection getConnection() {if(conn == null) {try {Class.forName(DRIVERNAME);conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);} catch (Exception e) {e.printStackTrace();}}return conn;}}
  
- 修改【UserDaoImpl】
  package com.gupao.dao.impl;import com.gupao.dao.IUserDao;import com.gupao.pojo.User;import com.gupao.utils.DbUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.RowMapper;import org.springframework.stereotype.Repository;import java.sql.*;import java.util.List;@Repositorypublic class UserDaoImpl implements IUserDao {private final String DRIVERNAME = "com.mysql.jdbc.Driver";private final String URL = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8";private final String USERNAME = "root";private final String PASSWORD = "123456";@Autowiredprivate JdbcTemplate template;private String sql;private Connection conn = null;private PreparedStatement ps = null;public Integer addUser(User user) throws SQLException {/*sql = "insert into users(name, age) values(?, ?)";int iRet = template.update(sql, user.getName(), user.getAge());*/int iRet = 0;conn = DbUtils.getConnection();sql = "insert into users(name, age) values(?, ?)";ps = conn.prepareStatement(sql);ps.setString(1, user.getName());ps.setInt(2, user.getAge());iRet = ps.executeUpdate();return iRet;}public Integer addUserInfo(String userName, String passwrod) throws Exception {/*sql = "insert into user_info(username, password) values(?, ?)";int iRet = template.update(sql, userName, passwrod);*/int iRet = 0;conn = DbUtils.getConnection();sql = "insert into user_info(username, password) values(?, ?)";ps = conn.prepareStatement(sql);ps.setString(1, userName);ps.setString(2, passwrod);iRet = ps.executeUpdate();return iRet;}public Integer transaction(User user, String username, String password) throws Exception {int iRet = 0;conn = DbUtils.getConnection();sql = "insert into users(name, age) values(?, ?)";ps = conn.prepareStatement(sql);ps.setString(1, user.getName());ps.setInt(2, user.getAge());iRet = ps.executeUpdate();sql = "insert into user_info(username, password) values(?, ?)";ps = conn.prepareStatement(sql);ps.setString(1, username);ps.setString(2, password);iRet += ps.executeUpdate();return iRet;}public List<User> query() {sql = "select * from users order by id desc";List<User> list = template.query(sql, new RowMapper<User>() {public User mapRow(ResultSet resultSet, int i) throws SQLException {User user = new User();try {user.setId(resultSet.getInt("id"));user.setName(resultSet.getString("name"));user.setAge(resultSet.getInt("age"));} catch (SQLException e) {e.printStackTrace();}return user;}});return list;}}
  
- 修改【IUserDao】
  package com.gupao.dao;import com.gupao.pojo.User;import java.sql.SQLException;import java.util.List;public interface IUserDao {public Integer addUser(User user) throws SQLException;public List<User> query();public Integer addUserInfo(String username, String password) throws Exception;public Integer transaction(User user, String username, String password) throws Exception;}
  
- 修改【UserServicImpl】
  package com.gupao.service.impl;import com.gupao.dao.IUserDao;import com.gupao.dao.impl.UserDaoImpl;import com.gupao.pojo.User;import com.gupao.service.IUserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class UserServicImpl implements IUserService {//@Autowiredprivate IUserDao userDao = new UserDaoImpl();public Integer addUser(User user) throws Exception {return userDao.addUser(user);}public List<User> query() {return userDao.query();}public Integer addUserInfo(String username, String password) throws Exception {return userDao.addUserInfo(username, password);}public Integer transaction(User user, String username, String password) throws Exception {//return userDao.tx(user, username, password);int iRet = 0;iRet = userDao.addUser(user);iRet += userDao.addUserInfo(username, password);return iRet;}}
  
- 修改【IUserService】
  package com.gupao.service;import com.gupao.pojo.User;import java.util.List;public interface IUserService {public Integer addUser(User user) throws Exception;public List<User> query();public Integer addUserInfo(String username, String password) throws Exception;public Integer transaction(User user, String username, String password) throws Exception;}
  
- 修改【JavaConfig】
  package com.gupao;import com.gupao.pojo.User;import com.gupao.service.IUserService;import com.gupao.service.impl.UserServicImpl;import com.gupao.utils.DbUtils;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.*;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.datasource.DriverManagerDataSource;import javax.sql.DataSource;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.sql.Connection;@Configuration@ComponentScanpublic class JavaConfig {@Beanpublic DataSource dataSource() {DriverManagerDataSource dataSource = new DriverManagerDataSource();dataSource.setDriverClassName("com.mysql.jdbc.Driver");dataSource.setUrl("jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8");dataSource.setUsername("root");dataSource.setPassword("123456");return dataSource;}@Bean@DependsOn("dataSource")public JdbcTemplate template(DataSource dataSource) {return new JdbcTemplate(dataSource);}public static void main(String[] args) {/*ApplicationContext ac = new AnnotationConfigApplicationContext(JavaConfig.class);IUserService bean = ac.getBean(IUserService.class);User user = new User();user.setName("灰灰");user.setAge(18);*//*System.out.println(bean.addUser(user));System.out.println(bean.addUserInfo("huihui", "123456"));*//*System.out.println(bean.tx(user, "huihui", "123456"));*/// 获取目标对象final IUserService target = new UserServicImpl();// 获取代理对象IUserService proxy = (IUserService) Proxy.newProxyInstance(JavaConfig.class.getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {Connection conn = null;try {conn = DbUtils.getConnection();conn.setAutoCommit(false);method.invoke(target, args[0], args[1], args[2]);// 目标对象执行System.out.println("执行成功 ... ... ");conn.commit();} catch (Exception e) {e.printStackTrace();System.out.println("执行失败,数据回滚 ... ");conn.rollback();} finally {conn.close();}return null;}});User user = new User();user.setName("灰灰");user.setAge(18);try {proxy.transaction(user, "huihui", "123456");} catch (Exception e) {e.printStackTrace();}}}
  
1.4 基于 Aspect J 的方式自定义事务
- 定义自定义注解
  package com.gupao.annotation;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;@Retention(RetentionPolicy.RUNTIME)public @interface GpTx {}
 
- 在【UserServicImpl】的【transaction】方法上添加【@GpTx】注解
  package com.gupao.service.impl;import com.gupao.annotation.GpTx;import com.gupao.dao.IUserDao;import com.gupao.dao.impl.UserDaoImpl;import com.gupao.pojo.User;import com.gupao.service.IUserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class UserServicImpl implements IUserService {//@Autowiredprivate IUserDao userDao = new UserDaoImpl();public Integer addUser(User user) throws Exception {return userDao.addUser(user);}public List<User> query() {return userDao.query();}public Integer addUserInfo(String username, String password) throws Exception {return userDao.addUserInfo(username, password);}@GpTxpublic Integer transaction(User user, String username, String password) throws Exception {//return userDao.tx(user, username, password);int iRet = 0;iRet = userDao.addUser(user);iRet += userDao.addUserInfo(username, password);return iRet;}}
  
- 自定义切面类【GpAspect】
  package com.gupao.aspect;import com.gupao.utils.DbUtils;import org.aspectj.lang.annotation.*;import org.springframework.stereotype.Component;import java.sql.Connection;import java.sql.SQLException;@Aspect@Componentpublic class GpAspect {/*** 事务处理的前置方法* @throws SQLException*/@Before(value = "@annotation(com.gupao.annotation.GpTx)")public void beforeTx() throws SQLException {Connection conn = DbUtils.getConnection();conn.setAutoCommit(false);}/*** 事务处理的后置通知* @throws SQLException*/@AfterReturning(value = "@annotation(com.gupao.annotation.GpTx)")public void afterReturningTx() throws SQLException {Connection conn = DbUtils.getConnection();System.out.println("执行成功 ... ... ");conn.commit();}/*** 事务处理的最终通知* @throws SQLException*/@After(value = "@annotation(com.gupao.annotation.GpTx)")public void afterTx() throws SQLException {Connection conn = DbUtils.getConnection();//conn.close();}@AfterThrowing(value = "@annotation(com.gupao.annotation.GpTx)")public void afterThrowingTx() throws SQLException {Connection conn = DbUtils.getConnection();System.out.println("失败回滚 ... ... ");conn.rollback();}}
  
- 【JavaConfig】添加【@EnableAspectJAutoProxy】类注解
  package com.gupao;import com.gupao.pojo.User;import com.gupao.service.IUserService;import com.gupao.service.impl.UserServicImpl;import com.gupao.utils.DbUtils;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.*;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.datasource.DriverManagerDataSource;import javax.sql.DataSource;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.sql.Connection;@Configuration@ComponentScan@EnableAspectJAutoProxypublic class JavaConfig {@Beanpublic DataSource dataSource() {DriverManagerDataSource dataSource = new DriverManagerDataSource();dataSource.setDriverClassName("com.mysql.jdbc.Driver");dataSource.setUrl("jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8");dataSource.setUsername("root");dataSource.setPassword("123456");return dataSource;}@Bean@DependsOn("dataSource")public JdbcTemplate template(DataSource dataSource) {return new JdbcTemplate(dataSource);}public static void main(String[] args) {/*ApplicationContext ac = new AnnotationConfigApplicationContext(JavaConfig.class);IUserService bean = ac.getBean(IUserService.class);User user = new User();user.setName("灰灰");user.setAge(18);*//*System.out.println(bean.addUser(user));System.out.println(bean.addUserInfo("huihui", "123456"));*//*System.out.println(bean.tx(user, "huihui", "123456"));*/// 获取目标对象final IUserService target = new UserServicImpl();// 获取代理对象IUserService proxy = (IUserService) Proxy.newProxyInstance(JavaConfig.class.getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {Connection conn = null;try {conn = DbUtils.getConnection();conn.setAutoCommit(false);method.invoke(target, args[0], args[1], args[2]);// 目标对象执行System.out.println("执行成功 ... ... ");conn.commit();} catch (Exception e) {e.printStackTrace();System.out.println("执行失败,数据回滚 ... ");conn.rollback();} finally {conn.close();}return null;}});User user = new User();user.setName("灰灰");user.setAge(18);try {proxy.transaction(user, "huihui", "123456");} catch (Exception e) {e.printStackTrace();}}}
  
- 添加【JavaConfigMain】
  package com.gupao;import com.gupao.pojo.User;import com.gupao.service.IUserService;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class JavaConfigMain {public static void main(String[] args) throws Exception {ApplicationContext ac = new AnnotationConfigApplicationContext(JavaConfig.class);IUserService bean = ac.getBean(IUserService.class);User user = new User();user.setName("令狐");user.setAge(18);bean.transaction(user, "linghu", "123456");}}
  
1.5 Spring 中的事务实现
- 新建 Maven 工程,添加依赖
  <dependencies><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>5.1.17.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.1.17.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><!-- 如果Mysql是8.0的就用这个驱动 --><!--<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.16</version></dependency>--><!-- 如果Mysql是5.7的就用这个驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.38</version></dependency><!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.1</version></dependency><!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version></dependency><!-- https://mvnrepository.com/artifact/log4j/log4j --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><!-- https://mvnrepository.com/artifact/logkit/logkit --><dependency><groupId>logkit</groupId><artifactId>logkit</artifactId><version>1.0.1</version></dependency><!-- https://mvnrepository.com/artifact/avalon-framework/avalon-framework --><dependency><groupId>avalon-framework</groupId><artifactId>avalon-framework</artifactId><version>4.1.5</version></dependency></dependencies>
  
- 新建【User】
  package com.gupao.pojo;public class User {private Integer id;private String userName;private Integer age;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public User(Integer id, String userName, Integer age) {this.id = id;this.userName = userName;this.age = age;}public User(String userName, Integer age) {this.userName = userName;this.age = age;}}
  
- 新建【IUserDao】
  package com.gupao.dao;import com.gupao.pojo.User;public interface IUserDao {public Integer addUser(User user) throws Exception;public Integer updateUser(User user) throws Exception;}
  
- 新建【UserDaoImpl】
  package com.gupao.dao.impl;import com.gupao.dao.IUserDao;import com.gupao.pojo.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.stereotype.Repository;@Repositorypublic class UserDaoImpl implements IUserDao {@Autowiredprivate JdbcTemplate template;private String sql;public Integer addUser(User user) throws Exception {sql = "insert into users(name, age) values(?, ?)";return template.update(sql, user.getUserName(), user.getAge());}public Integer updateUser(User user) throws Exception {sql = "update users set name=?, age=? where id=?";return template.update(sql, user.getUserName(), user.getAge(), user.getId());}}
  
- 新建【IUserService】
  package com.gupao.service;import com.gupao.pojo.User;public interface IUserService {public Integer addUser(User user) throws Exception;public Integer updateUser(User user) throws Exception;public void transaction() throws Exception;}
  
- 新建【UserServiceImpl】
package com.gupao.service.impl;import com.gupao.dao.IUserDao;import com.gupao.pojo.User;import com.gupao.service.IUserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class UserServiceImpl implements IUserService {@Autowiredprivate IUserDao dao;public Integer addUser(User user) throws Exception {return dao.addUser(user);}public Integer updateUser(User user) throws Exception {return dao.updateUser(user);}public void transaction() throws Exception {User user = new User("清扬", 18);addUser(user);user.setId(1);user.setUserName("零一");updateUser(user);}}
  
- 【Resource】下新建【db.properties】
  jdbc_driver=com.mysql.jdbc.Driverjdbc_url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8jdbc_username=rootjdbc_password=123456
  
- 【Resource】下新建【applicationContext.xml】
  <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"><context:component-scan base-package="com.gupao.*" /><!-- 加载数据配置文件 --><context:property-placeholder location="db.properties" /><!-- 配置数据源 --><bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource"><property name="driverClassName" value="${jdbc_driver}" /><property name="url" value="${jdbc_url}" /><property name="username" value="${jdbc_username}" /><property name="password" value="${jdbc_password}" /></bean><!-- 配置JdcbTemplate --><bean class="org.springframework.jdbc.core.JdbcTemplate"><constructor-arg name="dataSource" ref="dataSource" /></bean></beans>
  
- 新建【JavaConfig】
  package com.gupao;import com.gupao.service.IUserService;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class JavaConfig {public static void main(String[] args) throws Exception {ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");IUserService bean = ac.getBean(IUserService.class);bean.transaction();}}
 
1.5.1 基于 xml 文件
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"><context:component-scan base-package="com.gupao.*" /><!-- 加载数据配置文件 --><context:property-placeholder location="db.properties" /><!-- 配置数据源 --><bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource"><property name="driverClassName" value="${jdbc_driver}" /><property name="url" value="${jdbc_url}" /><property name="username" value="${jdbc_username}" /><property name="password" value="${jdbc_password}" /></bean><!-- 配置JdcbTemplate --><bean class="org.springframework.jdbc.core.JdbcTemplate"><constructor-arg name="dataSource" ref="dataSource" /></bean><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><tx:advice id="txAdvice" transaction-manager="txManager"><!-- the transactional semantics... --><tx:attributes><!-- all methods starting with 'get' are read-only --><tx:method name="tran*" propagation="REQUIRED" isolation="DEFAULT"/><!-- other methods use the default transaction settings (see below) --><tx:method name="*"/></tx:attributes></tx:advice><aop:config><aop:pointcut id="tx" expression="execution(* com.gupao.service..*.*(..))"/><aop:advisor advice-ref="txAdvice" pointcut-ref="tx"/></aop:config>
</beans>
  
1.5.2 基于注解
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"><context:component-scan base-package="com.gupao.*" /><!-- 加载数据配置文件 --><context:property-placeholder location="db.properties" /><!-- 配置数据源 --><bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource"><property name="driverClassName" value="${jdbc_driver}" /><property name="url" value="${jdbc_url}" /><property name="username" value="${jdbc_username}" /><property name="password" value="${jdbc_password}" /></bean><!-- 配置JdcbTemplate --><bean class="org.springframework.jdbc.core.JdbcTemplate"><constructor-arg name="dataSource" ref="dataSource" /></bean><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!-- 开启事务的注解支持 --><tx:annotation-driven transaction-manager="txManager" />
</beans>
  
- 添加【@Transactional】注解
package com.gupao.service.impl;import com.gupao.dao.IUserDao;
import com.gupao.pojo.User;
import com.gupao.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;@Service
public class UserServiceImpl implements IUserService {@Autowiredprivate IUserDao dao;public Integer addUser(User user) throws Exception {return dao.addUser(user);}public Integer updateUser(User user) throws Exception {return dao.updateUser(user);}@Transactionalpublic void transaction() throws Exception {User user = new User("清扬", 18);addUser(user);user.setId(1);user.setUserName("零一");updateUser(user);}}

这篇关于第九章 Spring5 高级应用及核心原理(三)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/843798

相关文章

Java五子棋之坐标校正

上篇针对了Java项目中的解构思维,在这篇内容中我们不妨从整体项目中拆解拿出一个非常重要的五子棋逻辑实现:坐标校正,我们如何使漫无目的鼠标点击变得有序化和可控化呢? 目录 一、从鼠标监听到获取坐标 1.MouseListener和MouseAdapter 2.mousePressed方法 二、坐标校正的具体实现方法 1.关于fillOval方法 2.坐标获取 3.坐标转换 4.坐

Spring Cloud:构建分布式系统的利器

引言 在当今的云计算和微服务架构时代,构建高效、可靠的分布式系统成为软件开发的重要任务。Spring Cloud 提供了一套完整的解决方案,帮助开发者快速构建分布式系统中的一些常见模式(例如配置管理、服务发现、断路器等)。本文将探讨 Spring Cloud 的定义、核心组件、应用场景以及未来的发展趋势。 什么是 Spring Cloud Spring Cloud 是一个基于 Spring

Javascript高级程序设计(第四版)--学习记录之变量、内存

原始值与引用值 原始值:简单的数据即基础数据类型,按值访问。 引用值:由多个值构成的对象即复杂数据类型,按引用访问。 动态属性 对于引用值而言,可以随时添加、修改和删除其属性和方法。 let person = new Object();person.name = 'Jason';person.age = 42;console.log(person.name,person.age);//'J

java8的新特性之一(Java Lambda表达式)

1:Java8的新特性 Lambda 表达式: 允许以更简洁的方式表示匿名函数(或称为闭包)。可以将Lambda表达式作为参数传递给方法或赋值给函数式接口类型的变量。 Stream API: 提供了一种处理集合数据的流式处理方式,支持函数式编程风格。 允许以声明性方式处理数据集合(如List、Set等)。提供了一系列操作,如map、filter、reduce等,以支持复杂的查询和转

Java面试八股之怎么通过Java程序判断JVM是32位还是64位

怎么通过Java程序判断JVM是32位还是64位 可以通过Java程序内部检查系统属性来判断当前运行的JVM是32位还是64位。以下是一个简单的方法: public class JvmBitCheck {public static void main(String[] args) {String arch = System.getProperty("os.arch");String dataM

详细分析Springmvc中的@ModelAttribute基本知识(附Demo)

目录 前言1. 注解用法1.1 方法参数1.2 方法1.3 类 2. 注解场景2.1 表单参数2.2 AJAX请求2.3 文件上传 3. 实战4. 总结 前言 将请求参数绑定到模型对象上,或者在请求处理之前添加模型属性 可以在方法参数、方法或者类上使用 一般适用这几种场景: 表单处理:通过 @ModelAttribute 将表单数据绑定到模型对象上预处理逻辑:在请求处理之前

eclipse运行springboot项目,找不到主类

解决办法尝试了很多种,下载sts压缩包行不通。最后解决办法如图: help--->Eclipse Marketplace--->Popular--->找到Spring Tools 3---->Installed。

JAVA读取MongoDB中的二进制图片并显示在页面上

1:Jsp页面: <td><img src="${ctx}/mongoImg/show"></td> 2:xml配置: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001

Java面试题:通过实例说明内连接、左外连接和右外连接的区别

在 SQL 中,连接(JOIN)用于在多个表之间组合行。最常用的连接类型是内连接(INNER JOIN)、左外连接(LEFT OUTER JOIN)和右外连接(RIGHT OUTER JOIN)。它们的主要区别在于它们如何处理表之间的匹配和不匹配行。下面是每种连接的详细说明和示例。 表示例 假设有两个表:Customers 和 Orders。 Customers CustomerIDCus

22.手绘Spring DI运行时序图

1.依赖注入发生的时间 当Spring loC容器完成了 Bean定义资源的定位、载入和解析注册以后,loC容器中已经管理类Bean 定义的相关数据,但是此时loC容器还没有对所管理的Bean进行依赖注入,依赖注入在以下两种情况 发生: 、用户第一次调用getBean()方法时,loC容器触发依赖注入。 、当用户在配置文件中将<bean>元素配置了 lazy-init二false属性,即让