public class BootStrap { public static void Start () { MySqlSession SQLSESSION = new new MySqlSession (); // outer SQLSESSION use, Mapper proxy generated, // TestMapper an interface, it is a dynamic agent, TestMapper testMapper = sqlSession.getMapper (. TestMapper class ); // Mapper to query, which calls the invoke method is to call sqlSession Mapper proxy, invoke a method which method to query the database, // query the database sqlSession method calls are methods of Executor, the Test the Test = testMapper.selectByPrimaryKey (. 1 ); System.out.println (Test); } public static void main(String[] args){ start(); } }
public class MySqlSession { Private the Executor Executor = new new SimpleExecutor (); public <T> T between selectOne (Statement String, Object Parameter) { return executor.query (Statement, Parameter); } public <T> T getMapper (Class <T> clazz ) { // clazz is an interface, return (T) the Proxy.newProxyInstance (clazz.getClassLoader (), new new Class [] {} clazz, new new MapperProxy ( the this , clazz)); // new new MapperProxy pass into the inside of the interface and SqlSession // the JDK dynamic proxy time, new MapperProxy proxy (to achieve additional content before and after class method), // new new MapperProxy parameter is the interface implementation class. // the mybatis did not pass into the implementation class, using sqlSession to query the database for true operation. // public class the implements the Advice InvocationHandler1 { // People people; // interface passed in Examples // public the Advice (People people) { // this.people = people; // } // public Invoke Object (Object Proxy, Method, Method, Object [] args) throws the Throwable { // before (); // front reinforcing // Object value = Method.invoke (people, args); // delegator // / After ();//后置增强 // return value; // } //} //public class MyTest { // public static void main(String[] args) { // People proxyObject = (People) Proxy1.newProxyInstance(MyTest.class.getClassLoader(), // new Class<?>[] { People.class }, new Advice(new Jack())); } }
public class MapperProxy<T> implements InvocationHandler { private final MySqlSession sqlSession; private final Class<T> mapperInterface; public MapperProxy(MySqlSession sqlSession, Class<T> mapperInterface) { this.sqlSession = sqlSession;//接口和sqlSession, this.mapperInterface = mapperInterface; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getDeclaringClass().getName().equals(TestMapperXml.nameSpace)) { String sql = TestMapperXml.methodSqlMapping.get(method.getName()); System.out.println(String.format("SQL [ %s ], parameter [%s] ", sql, args[0])); return sqlSession.selectOne(sql, String.valueOf(args[0])); } return null; } }
public interface Executor { <E> E query(String statement, Object parameter); }
public class SimpleExecutor implements Executor { public <E> E query(String sql, Object parameter) { try { Connection conn = getConnection(); PreparedStatement pstmt; pstmt = conn.prepareStatement(String.format(sql, Integer.parseInt(String.valueOf(parameter)))); ResultSet rs = pstmt.executeQuery(); Test test = new Test(); while (rs.next()) { test.setId(rs.getInt(1)); test.setName(rs.getString(2)); } return (E) test; } return null; } public Connection getConnection() throws SQLException { String driver = "com.mysql.cj.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/upgrade?useUnicode=true&characterEncoding=utf-8&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"; String username = "root"; String password = "123456"; Connection conn = null; try { Class.forName(driver); //classLoader,加载对应驱动 conn = DriverManager.getConnection(url, username, password); } return conn; } }
public class CachingExecutor implements Executor { private GpConfiguration configuration; private SimpleExecutor delegate; private Map<String,Object> localCache = new HashMap(); public CachingExecutor(SimpleExecutor delegate) { this.delegate = delegate; } public CachingExecutor(GpConfiguration configuration) { this.configuration = configuration; } public <E> E query(MapperRegistory.MapperData mapperData, Object parameter) throws Exception { //初始化StatementHandler --> ParameterHandler --> ResultSetHandler StatementHandler handler = new StatementHandler(configuration); Object result = localCache.get(mapperData.getSql()); if( null != result){ System.out.println("缓存命中"); return (E)result; } result = (E) delegate.query(mapperData,parameter); localCache.put(mapperData.getSql(),result); return (E)result; } }
public interface TestMapper { Test selectByPrimaryKey(Integer userId); }
public class TestMapperXml { public static final String nameSpace = "com.gupaoedu.mybatis.my.TestMapper"; public static final Map<String, String> methodSqlMapping = new HashMap<String, String>(); static { methodSqlMapping.put("selectByPrimaryKey", "select * from test where id = %d"); } }
Level cache is in update, delete the time will go to update the cache.
事务:都是用spring来管理事务的。 <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property value="jdbc:mysql://localhost:3306/upgrade?useSSL=false" name="url"/> <property name="username" value="root"/> <property name="password" value=""/> </dataSource> </environment> </environments>