码迷,mamicode.com
首页 > 其他好文 > 详细

MyBatis动态代理

时间:2017-12-22 23:54:50      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:icp   nts   unit   you   integer   oct   date   upd   last   

一、项目结构

技术分享图片

 

二、代码实现

技术分享图片
 1 import java.util.List;
 2 import java.util.Map;
 3 
 4 import com.jmu.bean.Student;
 5 
 6 public interface IStudentDao {
 7     void insertStudent(Student student);
 8     void insertStudentCacheId(Student student);// 插入后获取
 9     void deleteStudentById(int id);
10     void updateStudent(Student student);
11     List<Student> selectAllStudents();// 查询所有
12     /* Map<String,Object> selectAllStudentsMap(); */
13     Student selectStudentById(int id); // 根据id查询
14     List<Student> selectStudentsByName(String name);// 模糊查询
15 }
com.jmu.dao.IStudentDao
技术分享图片
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="com.jmu.dao.IStudentDao">
 6     <insert id="insertStudent" parameterType="Student">
 7         insert into student(name,age,score) values(#{name},#{age},#{score})
 8     </insert>
 9 
10     <insert id="insertStudentCacheId">
11         insert into student(name,age,score) values(#{name},#{age},#{score})
12         <selectKey resultType="int" keyProperty="id" order="AFTER">
13             select @@identity
14         </selectKey>
15     </insert>
16     <delete id="deleteStudentById">
17         delete from student where id=#{XXX}<!--这里的#{} 仅仅是个占位符,里边放什么都行 -->
18     </delete>
19     <update id="updateStudent">
20         update student set name=#{name},age=#{age},score=#{score} where id=#{id}
21     </update>
22     <select id="selectAllStudents" resultType="Student">
23         select id,name,age,score from student
24     </select>
25     <select id="selectStudentById" resultType="Student">
26         select id,name,age,score from student where id=#{JJJ}
27     </select>
28     <select id="selectStudentsByName" resultType="Student">
29         <!-- select id,name,age,score from student where name like CONCAT(‘%‘,#{XXX},‘%‘) -->
30         <!--     select id,name,age,score from student where name like ‘%王二%‘ -->
31         select id,name,age,score from student where name like ‘%${value}%‘ <!-- 表单传过来的值不能用该方式,sql注入 风险大-->
32         
33     </select>
34 </mapper>
/mybatis4-mapperDynamicProxy/src/com/jmu/dao/mapper.xml
技术分享图片
 1 import java.util.List;
 2 import org.apache.ibatis.session.SqlSession;
 3 import org.apache.log4j.BasicConfigurator;
 4 import org.junit.After;
 5 import org.junit.Before;
 6 import org.junit.Test;
 7 
 8 import com.jmu.bean.Student;
 9 import com.jmu.dao.IStudentDao;
10 import com.jmu.utils.MybatisUtils;
11 
12 public class MyTest {
13     private IStudentDao dao;
14     private SqlSession sqlSession;
15 
16     @Before
17     public void Before() {
18         sqlSession = MybatisUtils.getSqlSession();
19         dao = sqlSession.getMapper(IStudentDao.class);
20         BasicConfigurator.configure();
21     }
22   @After
23   public void after(){
24       if (sqlSession!=null) {
25           sqlSession.commit();
26         
27     }
28       
29   }
30     @Test
31     public void test01() {
32         Student student = new Student("明明", 19, 87.9);
33         System.out.println("插入前:student=" + student);
34         dao.insertStudent(student);
35         System.out.println("插入后:student=" + student);
36         sqlSession.commit();
37     }
38 
39     // 插入后获取
40     @Test
41     public void test02() {
42         Student student = new Student("明明", 23, 99.5);
43         System.out.println("插入前:student=" + student);
44         dao.insertStudentCacheId(student);
45         System.out.println("插入后:student=" + student);
46         sqlSession.commit();
47     }
48 
49     @Test
50     public void test03() {
51         dao.deleteStudentById(25);
52         sqlSession.commit();
53     }
54 
55     @Test
56     public void test04() {
57         Student student = new Student("红酒", 23, 93.5);
58         student.setId(28);
59         dao.updateStudent(student);
60         sqlSession.commit();
61     }
62 
63     @Test
64     public void test05() {
65         List<Student> students = dao.selectAllStudents();
66         for (Student student : students) {
67             System.out.println(student);
68         }
69     }
70 
71     /*@Test
72     public void test06() {
73         Map<String, Object> map = dao.selectAllStudentsMap();
74         System.out.println(map.get("王维"));
75     }*/
76 
77     @Test
78     public void test07() {
79         Student student = dao.selectStudentById(154);
80         System.out.println(student);
81     }
82 
83     @Test
84     public void test08() {
85         List<Student> students = dao.selectStudentsByName("明");
86         for (Student student : students) {
87             System.out.println(student);
88         }
89 
90     }
91 
92 }
com.jmu.test.MyTest
技术分享图片
 1 ### Global logging configuration
 2 log4j.logger.test=debug,console
 3 ##log4j.Logger.com.jmu.dao.IStudentDao=trace,console
 4 ### Uncomment for MyBatis logging
 5 log4j.logger.org.apache.ibatis=ERROR
 6 
 7 log4j.logger.org.apache.ibatis.session.AutoMappingUnknownColumnBehavior=WARN
 8 
 9 ### Console output...
10 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
11 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
12 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
13 
14 log4j.appender.lastEventSavedAppender=org.apache.ibatis.session.AutoMappingUnknownColumnBehaviorTest$LastEventSavedAppender
log4j.properties

 

输出:

技术分享图片
插入前: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(?,?,?) 
65 [main] DEBUG com.jmu.dao.IStudentDao.insertStudent  - ==> Parameters: 明明(String), 19(Integer), 87.9(Double)
66 [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]
103 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId  - ==>  Preparing: insert into student(name,age,score) values(?,?,?) 
103 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId  - ==>  Preparing: insert into student(name,age,score) values(?,?,?) 
103 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId  - ==> Parameters: 明明(String), 23(Integer), 99.5(Double)
103 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId  - ==> Parameters: 明明(String), 23(Integer), 99.5(Double)
104 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId  - <==    Updates: 1
104 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId  - <==    Updates: 1
107 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId!selectKey  - ==>  Preparing: select @@identity 
107 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId!selectKey  - ==>  Preparing: select @@identity 
107 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId!selectKey  - ==> Parameters: 
107 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId!selectKey  - ==> Parameters: 
144 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId!selectKey  - <==      Total: 1
144 [main] DEBUG com.jmu.dao.IStudentDao.insertStudentCacheId!selectKey  - <==      Total: 1
插入后:student=Student [id=161, name=明明, score=99.5, age=23]
162 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById  - ==>  Preparing: delete from student where id=? 
162 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById  - ==>  Preparing: delete from student where id=? 
162 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById  - ==>  Preparing: delete from student where id=? 
163 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById  - ==> Parameters: 25(Integer)
163 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById  - ==> Parameters: 25(Integer)
163 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById  - ==> Parameters: 25(Integer)
163 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById  - <==    Updates: 0
163 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById  - <==    Updates: 0
163 [main] DEBUG com.jmu.dao.IStudentDao.deleteStudentById  - <==    Updates: 0
183 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent  - ==>  Preparing: update student set name=?,age=?,score=? where id=? 
183 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent  - ==>  Preparing: update student set name=?,age=?,score=? where id=? 
183 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent  - ==>  Preparing: update student set name=?,age=?,score=? where id=? 
183 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent  - ==>  Preparing: update student set name=?,age=?,score=? where id=? 
184 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent  - ==> Parameters: 红酒(String), 23(Integer), 93.5(Double), 28(Integer)
184 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent  - ==> Parameters: 红酒(String), 23(Integer), 93.5(Double), 28(Integer)
184 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent  - ==> Parameters: 红酒(String), 23(Integer), 93.5(Double), 28(Integer)
184 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent  - ==> Parameters: 红酒(String), 23(Integer), 93.5(Double), 28(Integer)
185 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent  - <==    Updates: 0
185 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent  - <==    Updates: 0
185 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent  - <==    Updates: 0
185 [main] DEBUG com.jmu.dao.IStudentDao.updateStudent  - <==    Updates: 0
208 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents  - ==>  Preparing: select id,name,age,score from student 
208 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents  - ==>  Preparing: select id,name,age,score from student 
208 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents  - ==>  Preparing: select id,name,age,score from student 
208 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents  - ==>  Preparing: select id,name,age,score from student 
208 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents  - ==>  Preparing: select id,name,age,score from student 
209 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents  - ==> Parameters: 
209 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents  - ==> Parameters: 
209 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents  - ==> Parameters: 
209 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents  - ==> Parameters: 
209 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents  - ==> Parameters: 
224 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents  - <==      Total: 20
224 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents  - <==      Total: 20
224 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents  - <==      Total: 20
224 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents  - <==      Total: 20
224 [main] DEBUG com.jmu.dao.IStudentDao.selectAllStudents  - <==      Total: 20
Student [id=142, name=明明, score=87.9, age=19]
Student [id=143, name=明明, score=99.5, age=23]
Student [id=144, name=明明, score=87.9, age=19]
Student [id=145, name=明明, score=99.5, age=23]
Student [id=146, name=明明, score=87.9, age=19]
Student [id=147, name=明明, score=87.9, age=19]
Student [id=148, name=明明, score=87.9, age=19]
Student [id=149, name=明明, score=87.9, age=19]
Student [id=150, name=明明, score=87.9, age=19]
Student [id=151, name=明明, score=99.5, age=23]
Student [id=152, name=明明, score=87.9, age=19]
Student [id=153, name=明明, score=99.5, age=23]
Student [id=154, name=明明, score=99.5, age=23]
Student [id=155, name=明明, score=87.9, age=19]
Student [id=156, name=明明, score=87.9, age=19]
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=161, name=明明, score=99.5, age=23]
246 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById  - ==>  Preparing: select id,name,age,score from student where id=? 
246 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById  - ==>  Preparing: select id,name,age,score from student where id=? 
246 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById  - ==>  Preparing: select id,name,age,score from student where id=? 
246 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById  - ==>  Preparing: select id,name,age,score from student where id=? 
246 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById  - ==>  Preparing: select id,name,age,score from student where id=? 
246 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById  - ==>  Preparing: select id,name,age,score from student where id=? 
247 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById  - ==> Parameters: 154(Integer)
247 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById  - ==> Parameters: 154(Integer)
247 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById  - ==> Parameters: 154(Integer)
247 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById  - ==> Parameters: 154(Integer)
247 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById  - ==> Parameters: 154(Integer)
247 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById  - ==> Parameters: 154(Integer)
248 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById  - <==      Total: 1
248 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById  - <==      Total: 1
248 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById  - <==      Total: 1
248 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById  - <==      Total: 1
248 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById  - <==      Total: 1
248 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById  - <==      Total: 1
Student [id=154, name=明明, score=99.5, age=23]
304 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName  - ==>  Preparing: select id,name,age,score from student where name like ‘%明%‘ 
304 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName  - ==>  Preparing: select id,name,age,score from student where name like ‘%明%‘ 
304 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName  - ==>  Preparing: select id,name,age,score from student where name like ‘%明%‘ 
304 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName  - ==>  Preparing: select id,name,age,score from student where name like ‘%明%‘ 
304 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName  - ==>  Preparing: select id,name,age,score from student where name like ‘%明%‘ 
304 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName  - ==>  Preparing: select id,name,age,score from student where name like ‘%明%‘ 
304 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName  - ==>  Preparing: select id,name,age,score from student where name like ‘%明%‘ 
305 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName  - ==> Parameters: 
305 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName  - ==> Parameters: 
305 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName  - ==> Parameters: 
305 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName  - ==> Parameters: 
305 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName  - ==> Parameters: 
305 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName  - ==> Parameters: 
305 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName  - ==> Parameters: 
311 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName  - <==      Total: 20
311 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName  - <==      Total: 20
311 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName  - <==      Total: 20
311 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName  - <==      Total: 20
311 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName  - <==      Total: 20
311 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName  - <==      Total: 20
311 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByName  - <==      Total: 20
Student [id=142, name=明明, score=87.9, age=19]
Student [id=143, name=明明, score=99.5, age=23]
Student [id=144, name=明明, score=87.9, age=19]
Student [id=145, name=明明, score=99.5, age=23]
Student [id=146, name=明明, score=87.9, age=19]
Student [id=147, name=明明, score=87.9, age=19]
Student [id=148, name=明明, score=87.9, age=19]
Student [id=149, name=明明, score=87.9, age=19]
Student [id=150, name=明明, score=87.9, age=19]
Student [id=151, name=明明, score=99.5, age=23]
Student [id=152, name=明明, score=87.9, age=19]
Student [id=153, name=明明, score=99.5, age=23]
Student [id=154, name=明明, score=99.5, age=23]
Student [id=155, name=明明, score=87.9, age=19]
Student [id=156, name=明明, score=87.9, age=19]
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=161, name=明明, score=99.5, age=23]
output

 

MyBatis动态代理

标签:icp   nts   unit   you   integer   oct   date   upd   last   

原文地址:http://www.cnblogs.com/hoje/p/8087833.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!