MyBatis动态代理

时间:2023-03-09 01:13:55
MyBatis动态代理

一、项目结构

MyBatis动态代理

二、代码实现

 import java.util.List;
import java.util.Map; import com.jmu.bean.Student; public interface IStudentDao {
void insertStudent(Student student);
void insertStudentCacheId(Student student);// 插入后获取
void deleteStudentById(int id);
void updateStudent(Student student);
List<Student> selectAllStudents();// 查询所有
/* Map<String,Object> selectAllStudentsMap(); */
Student selectStudentById(int id); // 根据id查询
List<Student> selectStudentsByName(String name);// 模糊查询
}

com.jmu.dao.IStudentDao

 import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.BasicConfigurator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.jmu.bean.Student;
import com.jmu.dao.IStudentDao;
import com.jmu.utils.MybatisUtils; public class MyTest {
private IStudentDao dao;
private SqlSession sqlSession; @Before
public void Before() {
sqlSession = MybatisUtils.getSqlSession();
dao = sqlSession.getMapper(IStudentDao.class);
BasicConfigurator.configure();
}
@After
public void after(){
if (sqlSession!=null) {
sqlSession.commit(); } }
@Test
public void test01() {
Student student = new Student("明明", 19, 87.9);
System.out.println("插入前:student=" + student);
dao.insertStudent(student);
System.out.println("插入后:student=" + student);
sqlSession.commit();
} // 插入后获取
@Test
public void test02() {
Student student = new Student("明明", 23, 99.5);
System.out.println("插入前:student=" + student);
dao.insertStudentCacheId(student);
System.out.println("插入后:student=" + student);
sqlSession.commit();
} @Test
public void test03() {
dao.deleteStudentById(25);
sqlSession.commit();
} @Test
public void test04() {
Student student = new Student("红酒", 23, 93.5);
student.setId(28);
dao.updateStudent(student);
sqlSession.commit();
} @Test
public void test05() {
List<Student> students = dao.selectAllStudents();
for (Student student : students) {
System.out.println(student);
}
} /*@Test
public void test06() {
Map<String, Object> map = dao.selectAllStudentsMap();
System.out.println(map.get("王维"));
}*/ @Test
public void test07() {
Student student = dao.selectStudentById(154);
System.out.println(student);
} @Test
public void test08() {
List<Student> students = dao.selectStudentsByName("明");
for (Student student : students) {
System.out.println(student);
} } }

com.jmu.test.MyTest

 ### Global logging configuration
log4j.logger.test=debug,console
##log4j.Logger.com.jmu.dao.IStudentDao=trace,console
### Uncomment for MyBatis logging
log4j.logger.org.apache.ibatis=ERROR log4j.logger.org.apache.ibatis.session.AutoMappingUnknownColumnBehavior=WARN ### Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n log4j.appender.lastEventSavedAppender=org.apache.ibatis.session.AutoMappingUnknownColumnBehaviorTest$LastEventSavedAppender

log4j.properties

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jmu.dao.IStudentDao">
<insert id="insertStudent" parameterType="Student">
insert into
student(name,age,score) values(#{name},#{age},#{score})
</insert> <insert id="insertStudentCacheId">
insert into student(name,age,score) values(#{name},#{age},#{score})
<selectKey resultType="int" keyProperty="id" order="AFTER">
select
@@identity
</selectKey>
</insert>
<delete id="deleteStudentById">
delete from student where id=#{XXX}<!--这里的#{} 仅仅是个占位符,里边放什么都行 -->
</delete>
<update id="updateStudent">
update student set
name=#{name},age=#{age},score=#{score} where id=#{id}
</update>
<select id="selectAllStudents" resultType="Student">
select
id,name,age,score from student
</select>
<select id="selectStudentById" resultType="Student">
select
id,name,age,score from student where id=#{JJJ}
</select>
<select id="selectStudentsByName" resultType="Student">
<!-- select id,name,age,score from student where name like CONCAT('%',#{XXX},'%') -->
<!-- select id,name,age,score from student where name like '%王二%' -->
<!-- select id,name,age,score from student where name like '%${value}%' --><!-- 不存在sql注入 风险,但是效率低 -->
select id,name,age,score from student where name like '%' #{XXX} '%'<!--常用 -->
</select>
</mapper>

/mybatis4-mapperDynamicProxy/src/com/jmu/dao/mapper.xml

输出:

插入前:student=Student [id=null, name=明明, score=87.9, age=19]
0 [main] DEBUG com.jmu.dao.IStudentDao.insertStudent - ==> Preparing: insert into student(name,age,score) values(?,?,?)
56 [main] DEBUG com.jmu.dao.IStudentDao.insertStudent - ==> Parameters: 明明(String), 19(Integer), 87.9(Double)
57 [main] DEBUG com.jmu.dao.IStudentDao.insertStudent - <== Updates: 1
插入后:student=Student [id=null, name=明明, score=87.9, age=19]
插入前:student=Student [id=null, name=明明, score=99.5, age=23]
82 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId - ==> Preparing: insert into student(name,age,score) values(?,?,?)
82 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId - ==> Preparing: insert into student(name,age,score) values(?,?,?)
83 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId - ==> Parameters: 明明(String), 23(Integer), 99.5(Double)
83 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId - ==> Parameters: 明明(String), 23(Integer), 99.5(Double)
85 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId - <== Updates: 1
85 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId - <== Updates: 1
88 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId!selectKey - ==> Preparing: select @@identity
88 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId!selectKey - ==> Preparing: select @@identity
88 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId!selectKey - ==> Parameters:
88 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId!selectKey - ==> Parameters:
126 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId!selectKey - <== Total: 1
126 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId!selectKey - <== Total: 1
插入后:student=Student [id=165, name=明明, score=99.5, age=23]
145 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById - ==> Preparing: delete from student where id=?
145 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById - ==> Preparing: delete from student where id=?
145 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById - ==> Preparing: delete from student where id=?
145 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById - ==> Parameters: 25(Integer)
145 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById - ==> Parameters: 25(Integer)
145 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById - ==> Parameters: 25(Integer)
146 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById - <== Updates: 0
146 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById - <== Updates: 0
146 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById - <== Updates: 0
162 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - ==> Preparing: update student set name=?,age=?,score=? where id=?
162 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - ==> Preparing: update student set name=?,age=?,score=? where id=?
162 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - ==> Preparing: update student set name=?,age=?,score=? where id=?
162 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - ==> Preparing: update student set name=?,age=?,score=? where id=?
163 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - ==> Parameters: 红酒(String), 23(Integer), 93.5(Double), 28(Integer)
163 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - ==> Parameters: 红酒(String), 23(Integer), 93.5(Double), 28(Integer)
163 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - ==> Parameters: 红酒(String), 23(Integer), 93.5(Double), 28(Integer)
163 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - ==> Parameters: 红酒(String), 23(Integer), 93.5(Double), 28(Integer)
163 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - <== Updates: 0
163 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - <== Updates: 0
163 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - <== Updates: 0
163 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent - <== Updates: 0
181 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - ==> Preparing: select id,name,age,score from student
181 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - ==> Preparing: select id,name,age,score from student
181 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - ==> Preparing: select id,name,age,score from student
181 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - ==> Preparing: select id,name,age,score from student
181 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - ==> Preparing: select id,name,age,score from student
182 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - ==> Parameters:
182 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - ==> Parameters:
182 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - ==> Parameters:
182 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - ==> Parameters:
182 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - ==> Parameters:
188 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - <== Total: 8
188 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - <== Total: 8
188 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - <== Total: 8
188 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - <== Total: 8
188 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents - <== Total: 8
Student [id=157, name=明明, score=87.9, age=19]
Student [id=158, name=明明, score=87.9, age=19]
Student [id=159, name=明明, score=87.9, age=19]
Student [id=160, name=明明, score=87.9, age=19]
Student [id=162, name=明明, score=87.9, age=19]
Student [id=163, name=明明, score=99.5, age=23]
Student [id=164, name=明明, score=87.9, age=19]
Student [id=165, name=明明, score=99.5, age=23]
206 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Preparing: select id,name,age,score from student where id=?
206 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Preparing: select id,name,age,score from student where id=?
206 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Preparing: select id,name,age,score from student where id=?
206 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Preparing: select id,name,age,score from student where id=?
206 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Preparing: select id,name,age,score from student where id=?
206 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Preparing: select id,name,age,score from student where id=?
207 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Parameters: 157(Integer)
207 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Parameters: 157(Integer)
207 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Parameters: 157(Integer)
207 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Parameters: 157(Integer)
207 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Parameters: 157(Integer)
207 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Parameters: 157(Integer)
211 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - <== Total: 1
211 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - <== Total: 1
211 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - <== Total: 1
211 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - <== Total: 1
211 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - <== Total: 1
211 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - <== Total: 1
Student [id=157, name=明明, score=87.9, age=19]
247 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Preparing: select id,name,age,score from student where name like '%' ? '%'
247 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Preparing: select id,name,age,score from student where name like '%' ? '%'
247 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Preparing: select id,name,age,score from student where name like '%' ? '%'
247 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Preparing: select id,name,age,score from student where name like '%' ? '%'
247 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Preparing: select id,name,age,score from student where name like '%' ? '%'
247 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Preparing: select id,name,age,score from student where name like '%' ? '%'
247 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Preparing: select id,name,age,score from student where name like '%' ? '%'
253 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Parameters: 明(String)
253 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Parameters: 明(String)
253 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Parameters: 明(String)
253 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Parameters: 明(String)
253 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Parameters: 明(String)
253 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Parameters: 明(String)
253 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - ==> Parameters: 明(String)
262 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - <== Total: 8
262 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - <== Total: 8
262 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - <== Total: 8
262 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - <== Total: 8
262 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - <== Total: 8
262 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - <== Total: 8
262 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName - <== Total: 8
Student [id=157, name=明明, score=87.9, age=19]
Student [id=158, name=明明, score=87.9, age=19]
Student [id=159, name=明明, score=87.9, age=19]
Student [id=160, name=明明, score=87.9, age=19]
Student [id=162, name=明明, score=87.9, age=19]
Student [id=163, name=明明, score=99.5, age=23]
Student [id=164, name=明明, score=87.9, age=19]
Student [id=165, name=明明, score=99.5, age=23]

output