JAVA framework Mybaits dynamic proxy

 1. Dynamic proxy:

mybaits provides us with a set of dynamic agents, we only need to write the interface according to his requirements, mybatis helps to do the dynamic agent, which is equivalent to the implementation class of the interface we wrote. The bottom layer instantiates the proxy object through reflection, and calls the corresponding method through the proxy object.

Execute the appropriate database statement.

interface:

 1 package jd.com.proxydao;
 2 
 3 
 4 import jd.com.mybaitstest.account;
 5 
 6 import java.io.IOException;
 7 import java.util.List;
 8 
 9 public interface AccMapper {
10     List<account>  selectDemo(String name) throws IOException;
11      void  updateDemo(account ac) throws IOException;
12 }

mapper file:

 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="jd.com.proxydao.AccMapper" >
 6     <select id="selectDemo" resultType="jd.com.mybaitstest.account" parameterType="java.lang.String" >
 7 
 8         SELECT * FROM  t_account WHERE NAME LIKE '%${value}%';
 9     </select>
10     <insert id="updateDemo" parameterType="jd.com.mybaitstest.account">
11 
12         <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
13 
14             SELECT LAST_INSERT_ID();
15         </selectKey>
16         INSERT INTO t_account(NAME ,money) VALUES(#{name},#{money});
17     </insert>
18 </mapper>

 Note: the mapper file and the interface are in the same directory!

Test code:

 1 package jd.com.proxydao;
 2 
 3 import jd.com.mybaitstest.account;
 4 import jd.com.Utils.session;
 5 import org.apache.ibatis.session.SqlSession;
 6 import org.junit.jupiter.api.Test;
 7 
 8 import java.io.IOException;
 9 import java.util.List;
10 
11 public class TestDemo {
12     @Test
13     public void  testdemo1() throws IOException {
14         SqlSession sqlSession=session.getSession();
15         AccMapper ac=sqlSession.getMapper(AccMapper.class);
16         List<account> acc1=ac.selectDemo("ok");
17         account acz=new account();
18         acz.setId(9);
19         acz.setMoney(2313131);
20         ac.updateDemo(acz);
21         sqlSession.commit();
22         sqlSession.close();
23         System.out.println("acc1 = " + acc1);
24     }
25 }

 

Tools:

 1 package jd.com.Utils;
 2 
 3 import org.apache.ibatis.io.Resources;
 4 import org.apache.ibatis.session.SqlSession;
 5 import org.apache.ibatis.session.SqlSessionFactory;
 6 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 7 
 8 import java.io.IOException;
 9 import java.io.InputStream;
10 
11 public class session {
12     private  static  SqlSessionFactory sqlSessionFactory;
13 
14     public static  SqlSession getSession(){
15         String resource="SqlMapConfig.xml";
16         try {
17             InputStream inp= Resources.getResourceAsStream(resource);
18             sqlSessionFactory= new SqlSessionFactoryBuilder().build(inp);
19             SqlSession sqlSession=sqlSessionFactory.openSession();
20             return  sqlSession;
21         } catch (Exception e) {
22             e.printStackTrace();
23         }
24         return  null;
25     }
26 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324780372&siteId=291194637