Query all the data in a table.
Environmental Science:
Use the tool IntelliJ IDEA version 2018.2.
Creating Maven Project without Skeleton
1.pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.jxjdemo</groupId> 8 <artifactId>day33_mybatis1_quicksta</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <properties> 12 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 13 <maven.compiler.source>1.8</maven.compiler.source> 14 <maven.compiler.target>1.8</maven.compiler.target> 15 </properties> 16 17 <dependencies> 18 <!--MySql data base--> 19 <dependency><!-- rely on --> 20 <groupId>mysql</groupId><!--Company name/Organization name groupId: Domain name reversal --> 21 <artifactId>mysql-connector-java</artifactId><!--Project package/Package name --> 22 <version>5.1.47</version><!--Version number version: 1.0-SNAPSHOT Development Version 1.0-RELEASE Stable Release --> 23 </dependency> 24 <!--MyBatis Of jar package--> 25 <dependency> 26 <groupId>org.mybatis</groupId> 27 <artifactId>mybatis</artifactId> 28 <version>3.5.2</version> 29 </dependency> 30 <!--Log pack--> 31 <dependency> 32 <groupId>log4j</groupId> 33 <artifactId>log4j</artifactId> 34 <version>1.2.17</version> 35 </dependency> 36 <!--unit testing--> 37 <dependency> 38 <groupId>junit</groupId> 39 <artifactId>junit</artifactId> 40 <version>4.12</version> 41 <scope>test</scope> <!--plus test Unit tests can only be written in test within--> 42 </dependency> 43 </dependencies> 44 45 </project>
2. table - class
1 package com.jxjdemo.domain; 2 3 import java.util.Date; 4 5 public class User { 6 private Integer id; 7 private String username; 8 private Date birthday; //Guide package, frame automatically help us to transfer can be written in this way. 9 private String sex; 10 private String address; 11 12 @Override 13 public String toString() { 14 return "User{" + 15 "id=" + id + 16 ", username='" + username + '\'' + 17 ", birthday=" + birthday + 18 ", sex='" + sex + '\'' + 19 ", address='" + address + '\'' + 20 '}'; 21 } 22 //ellipsis Get And Set Method
3. mapper
1 package com.jxjdemo.dao; 2 import com.jxjdemo.domain.User; 3 import java.util.List; 4 /** 5 * Mapper: interface of dao layer 6 */ 7 public interface UserDao { 8 List<User> queryAll(); 9 }
4. Mapper Profile
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 <!-- 6 The constraints introduced above 7 _____________________________________________________________ 8 The following mapper: Mapper configuration 9 namespace: Fully qualified class name of the mapper 10 --> 11 <mapper namespace="com.jxjdemo.dao.UserDao"> 12 <!-- 13 statement: The configuration information for each method in the mapper is called statement 14 select tag: for query 15 insert tag: for insertion 16 update tag: for modification 17 delete tag: for deletion 18 More than 19 Tags have attributes: 20 id: Name of write method in mapper Types of 21 resultType result set encapsulation 22 --> 23 <select id="queryAll" resultType="com.jxjdemo.domain.User"> 24 select * from user 25 </select> 26 </mapper>
5. Database Core Profile
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <! - Mybatis core configuration file, mainly configuring database connection information - > 2. 6 < configuration > <! - root tag - > < 7 <! -- Enxironments can configure multiple database environments - > Enxironments can configure multiple database environments. 8 <environments default="mysql"><!--default default database 9 < environment id = "mysql"> <! - environment information for each database connection (configuration) - - > 10 < Transaction Manager type = "JDBC"/> <! - Way of managing things - > JDBC 11 < dataSource type = "POOLED"> <! - Data source. POOLED does not use UN connection pool, POOLED uses connection pool, JNDI finds data source configuration file - >. 12 <property name="driver" value="com.mysql.jdbc.Driver"/> 13 <property name= "url" value= "jdbc: mysql://localhost: port number/library name"/> 14 <property name="username" value="account"/> 15 <property name= "password" value= "password"/> 16 </dataSource> 17 </environment> 18 </environments> 19 < mappers > <! - mappers - > mappers 20 <mapper resource="com/jxjdemo/dao/UserDao.xml" /> 21 </mappers> 22 </configuration>
6. test class
1 package com.jxjtest.test; 2 3 import com.jxjdemo.dao.UserDao; 4 import com.jxjdemo.domain.User; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 import org.junit.Test; 11 12 import java.io.IOException; 13 import java.io.InputStream; 14 import java.util.List; 15 16 public class QuickstartTest { //Quickstart Quick start 17 @Test 18 public void testQuickStart() throws IOException { 19 //1.Read configuration files 20 InputStream is = Resources.getResourceAsStream("sqlMapConfig.xml"); //Throw exception 21 22 //2.Read to Sqlsession object.From SqlSessionFactory Inside production SqlSession object 23 SqlSessionFactory facyory = new SqlSessionFactoryBuilder().build(is); 24 //new A factory, pass it on to him, return a factory object 25 SqlSession session = facyory.openSession();//With factory objects, use openSession obtain session 26 27 //3.Operating database 28 UserDao userDao = session.getMapper(UserDao.class);//Use getMapper Return UserDao Object, dynamic proxy has interface. Creating proxy objects 29 List<User> userList = userDao.queryAll(); 30 for (User user : userList) { 31 System.out.println(user); 32 } 33 //4.Release resources 34 session.close(); 35 is.close(); 36 } 37 }
7. Project structure
8. At the test class breakpoint, start deBug tracking, and start here. First we get UserDao, which is a proxy object.
1 UserDao userDao = session.getMapper(UserDao.class);//Use getMapper Return UserDao object
2 List<User> userList = userDao.queryAll();
9. The invole method in MapperProxy.java
1 @Override 2 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 3 try { 4 if (Object.class.equals(method.getDeclaringClass())) {//1. 5 return method.invoke(this, args); 6 } else if (method.isDefault()) {//2. 7 return invokeDefaultMethod(proxy, method, args); 8 } 9 } catch (Throwable t) { 10 throw ExceptionUtil.unwrapThrowable(t); 11 } 12 final MapperMethod mapperMethod = cachedMapperMethod(method);//3. 13 return mapperMethod.execute(sqlSession, args);//4.Method entry method to start executing mapper 14 }
10. In MapperMethod.java
1 public Object execute(SqlSession sqlSession, Object[] args) { 2 Object result; 3 switch (command.getType()) {//1.Judging landing type 4 case INSERT: { 5 Object param = method.convertArgsToSqlCommandParam(args); 6 result = rowCountResult(sqlSession.insert(command.getName(), param)); 7 break; 8 } 9 case UPDATE: { 10 Object param = method.convertArgsToSqlCommandParam(args); 11 result = rowCountResult(sqlSession.update(command.getName(), param)); 12 break; 13 } 14 case DELETE: { 15 Object param = method.convertArgsToSqlCommandParam(args); 16 result = rowCountResult(sqlSession.delete(command.getName(), param)); 17 break; 18 } 19 case SELECT: 20 if (method.returnsVoid() && method.hasResultHandler()) { 21 executeWithResultHandler(sqlSession, args);//2.Executing queries is select 22 result = null; 23 } else if (method.returnsMany()) {//3.According to the method return value, returnsMany()Return multiple 24 result = executeForMany(sqlSession, args);//4.Tune up executeForMany Get into 25 } else if (method.returnsMap()) { 26 result = executeForMap(sqlSession, args); 27 } else if (method.returnsCursor()) { 28 result = executeForCursor(sqlSession, args); 29 } else { 30 Object param = method.convertArgsToSqlCommandParam(args); 31 result = sqlSession.selectOne(command.getName(), param); 32 if (method.returnsOptional() 33 && (result == null || !method.getReturnType().equals(result.getClass()))) { 34 result = Optional.ofNullable(result); 35 } 36 } 37 break;
11. To execute ForMany
1 private <E> Object executeForMany(SqlSession sqlSession, Object[] args) { 2 List<E> result; 3 Object param = method.convertArgsToSqlCommandParam(args);//1.Parameters of preparation method 4 if (method.hasRowBounds()) {//2.Because the query is complete, there are no parameters, so the parameters are null 5 RowBounds rowBounds = method.extractRowBounds(args); 6 result = sqlSession.selectList(command.getName(), param, rowBounds); 7 } else { 8 result = sqlSession.selectList(command.getName(), param);//Here we are. setlectList And 9 }
12. For the rest, see the screenshot of the steps. Start with https://www.cnblogs.com/jxearlier/p/11625253.html in 11 steps.
13. The complete process is as follows.