org.springframework spring-orm 5.2.20.RELEASE |
4.0.0 com.hy ssm01 0.0.1 war UTF-8 UTF-8 UTF-8 org.springframework spring-context 5.2.20.RELEASE org.springframework spring-orm 5.2.20.RELEASE org.springframework spring-aspects 5.2.20.RELEASE org.mybatis mybatis 3.5.6 org.mybatis mybatis-spring 2.0.7 org.projectlombok lombok 1.18.20 com.alibaba druid 1.2.9 mysql mysql-connector-java 5.1.49 org.springframework spring-test 5.2.20.RELEASE junit junit 4.12 test ch.qos.logback logback-classic 1.2.3 org.mybatis.caches mybatis-ehcache 1.2.1 org.apache.maven.plugins maven-compiler-plugin 1.8 1.8 UTF-8 |
[%d{HH:mm:ss.SSS}] [%-5level] [%thread] [%logger] [%msg]%n |
package com.hy.test; import java.sql.Connection; import java.sql.SQLException; import javax.sql.DataSource; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring-mybatis.xml") public class Test01 { @Autowired private DataSource dataSource; private Logger logger = LoggerFactory.getLogger(this.getClass()); @Test public void testConnection() throws SQLException { Connection connection = dataSource.getConnection(); logger.debug(connection.toString()); } } |
package com.hy.bean; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import lombok.ToString; @NoArgsConstructor @AllArgsConstructor @Setter @Getter @ToString public class Emp { private Long empId; //用包装类有null值 private String empName; private String empPwd; private String empGender; private Double empSalary; //用包装类有null值 //构造方法(去ID的) public Emp(String empName, String empPwd, String empGender, Double empSalary) { super(); this.empName = empName; this.empPwd = empPwd; this.empGender = empGender; this.empSalary = empSalary; } } |
package com.hy.mapper; import java.util.List; import java.util.Map; import com.hy.bean.Emp; public interface EmpMapper { abstract public Emp selectById(long empId); abstract public int insert(Emp emp); abstract public int deleteById(long empId); abstract public int update(Emp emp); abstract public int updateByMap(Map paramMap); abstract public Integer selectCount(); abstract public Map selectForMap(int empId); abstract public List selectAll(); abstract public int insertWithKey(Emp emp); } |
select emp_id empId,emp_name empName,emp_pwd empPwd,emp_gender empGender , emp_salary empSalary from sys_emp where emp_id = #{empId} |
package com.hy.test; import java.sql.SQLException; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.hy.bean.Emp; import com.hy.mapper.EmpMapper; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring-mybatis.xml") public class Test02 { @Autowired private EmpMapper empMapper; private Logger logger = LoggerFactory.getLogger(this.getClass()); @Test public void testEmpMapper () throws SQLException { Emp emp = empMapper.selectById(1); logger.debug(emp.toString()); } } |
Connection conn = ...; try { // 开启事务:关闭事务的自动提交 conn.setAutoCommit(false); // 核心操作 // 提交事务 conn.commit(); }catch(Exception e){ // 回滚事务 conn.rollBack(); }finally{ // 释放数据库连接 conn.close(); } |
package com.hy.service; import com.hy.bean.Emp; public interface EmpService { abstract public Emp listById(long empId); } |
@Service public class EmpServiceImpl implements EmpService{ @Autowired private EmpMapper empMapper; public Emp listById(long empId) { Emp emp = empMapper.selectById(empId); return emp; } } |
package com.hy.test; import java.sql.SQLException; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.hy.bean.Emp; import com.hy.service.EmpService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring-mybatis.xml") public class Test03 { @Autowired private EmpService empService; private Logger logger = LoggerFactory.getLogger(this.getClass()); @Test public void testEmpService() throws SQLException { Emp emp = empService.listById(1); logger.debug(emp.toString()); } } |
package com.hy.mapper; import java.util.List; import java.util.Map; import org.apache.ibatis.annotations.Param; import com.hy.bean.Emp; public interface EmpMapper { abstract public void updateEmpNameById(@Param("empId") long empId, @Param("empName")String empName); abstract public void updateEmpSalaryById(@Param("empId") long empId, @Param("empSalary")Double empSalary); abstract public Emp selectById(long empId); } |
select emp_id empId,emp_name empName,emp_pwd empPwd,emp_gender empGender , emp_salary empSalary from sys_emp where emp_id = #{empId} update sys_emp set emp_name = #{empName} where emp_id = #{empId} update sys_emp set emp_salary = #{empSalary} where emp_id = #{empId} |
package com.hy.service; import com.hy.bean.Emp; public interface EmpService { abstract public Emp listById(long empId); abstract public int eidtEmp(Emp emp); } |
package com.hy.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.hy.bean.Emp; import com.hy.mapper.EmpMapper; import com.hy.service.EmpService; @Service public class EmpServiceImpl implements EmpService{ @Autowired private EmpMapper empMapper; public Emp listById(long empId) { Emp emp = empMapper.selectById(empId); return emp; } @Override public int eidtEmp(Emp emp) { empMapper.updateEmpNameById(emp.getEmpId(), emp.getEmpName()); empMapper.updateEmpSalaryById(emp.getEmpId(), emp.getEmpSalary()); return 0; } } |
package com.hy.test; import java.sql.SQLException; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.hy.bean.Emp; import com.hy.service.EmpService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring-mybatis.xml") public class Test03 { @Autowired private EmpService empService; private Logger logger = LoggerFactory.getLogger(this.getClass()); @Test public void testEmpService() throws SQLException { Emp emp = empService.listById(1); logger.debug(emp.toString()); } @Test public void testEmpService2() throws SQLException { Emp emp = new Emp(1L, "范冰冰plus3", "fbbplus", "f", 1315d); empService.eidtEmp(emp); } } |
Connection conn = ...; try { // 开启事务:关闭事务的自动提交 conn.setAutoCommit(false); // 核心操作 // 提交事务 conn.commit(); }catch(Exception e){ // 回滚事务 conn.rollBack(); }finally{ // 释放数据库连接 conn.close(); } |
org.springframework spring-orm 5.2.20.RELEASE |
package com.hy.service; public interface EmpService { abstract public Emp listById(long empId); } |
package com.hy.service.impl; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.hy.mapper.EmpMapper; import com.hy.service.EmpService; @Service public class EmpServiceImpl implements EmpService { @Autowired private EmpMapper empMapper; @Transactional @Override public Emp listById(long empId) { Emp emp = empMapper.selectById(empId); return emp; } } |
加了只读注解后,会有哪些影响呢? 比如做报表或者做统计: 只读事务的好处,作为ORM框架优化的暗号,保证读一致性,事务内不允许DML操作,否则报错 只读事务的场景,如统计,保证统计结果准确性。 只读事务里,也可以在只读事务里使用 select... for update 因为只读事务,所有查询都是在一个事务里,所以可以配合mysql的事务隔离级别理解一下 (比如,你的mysql隔离事务是RR的,那么在只读事务注解里,多次查询同一个表的范围数据, 结果是一致的,如果不是在同一个事务里,那么前后查询可能会读到的数据条数不一致,造成幻读),如果隔离级别是RC的,可以不用在只读事务里,因为每次查询都会读取到已提交的数据 |
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) | Powered by Discuz! X3.4 |