MyBatis Entry 1, MyBatis entry
1. MyBatis
MyBatis is an open-source data persistence layer framework. Its main idea is to strip a large number of SQL statements from the program and configure them in the configuration file to implement flexible SQL configuration. And without modifying the program code
Directly modify the SQL statement in the configuration file.
2. What is ORM?
Orm (object/Relation Mapping) object/link ing. It is a data persistence technology that establishes a correspondence between the object model and the relational database, and operates the data in the database table through the javaBean object.
3. Create the MyBatis core configuration file configureation. xml
---- Case Configuration
Xml node: https://www.geeksss.com/article-298-1.html
Complete configure Configuration
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE configurationPUBLIC "-// mybatis.org//DTD Config 3.0 //" http://mybatis.org/dtd/mybatis-3-config.dtd "> <configuration> <! -- Set some running parameters of mtbatis: log, whether to output directly to the console or to the log4j file --> <settings> <! -- Name cannot be written at will. It can only be called; value has several options. Here, select output to the console --> <setting name = "logImpl" value = "STDOUT_LOGGING"/> </settings> <! -- Set the type alias: If the alias is not set during SQL statement execution. Each time you specify a package name + class name, the write length --> <typeAliases> <! -- Type is the complete object class name, --> <tapeAlias type = "entity. student" alias = "User"/> </typeAliases> <! -- Database connection attribute. different development stages are connected to different database development stages -- Development Environment: connection to the programmer's own database test stage -- test environment: connection to the test database for online running -- production environment: you can write multiple environment statements to connect to the official database --> <environments default = "dev"> <! -- Development environment --> <environment id = "dev"> <! -- Manage transactions through JDBC --> <transactionManager type = "JDBC"/> <! -- Do not use the connection pool --> <dataSource type = "UNPOOLED"> <property name = "driver" value = "com. mysql. jdbc. driver "/> <property name =" url "value =" jdbc: mysql: // localhost: 3306: myschool "/> <property name =" username "value =" root "/> <property name =" password "value =" 0217 "/> </dataSource> </environment> </environments> <! -- Path of the SQL ing file --> <mappers> <! -- Introduce the SQL file to be loaded --> <mapper resource = "studentDao. xml"/> </mappers> </configuration>
Mapper ing Configuration
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE mapper PUBLIC "-// mybatis.org//DTD Mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <mapper namespace =" dao. studentDao "> <! -- ResultMap --> <resultMap id = "studentMap" type = "entity. student "> <id property =" stuId "column =" stuId "> </id> <result property =" stuName "column =" stuName "> </result> </resultMap> <! -- Query all --> <select id = "getAllStudent" resultMap = "studentMap"> <! -- ResultMap corresponds to the id value in resultMap --> select * from student </select> <! -- Query by id value --> <select id = "getStudentById" parameterType = "int" resultType = "stu"> select * from student where stuId = # {stuId} </select> </mapper>
4. Writing Tools
Public class MyBatis {private static final String RESOURCE = "mybatis-config.xml"; private static SqlSessionFactory sqlSessionFactory = null; private static ThreadLocal <SqlSession> threadLocal = new ThreadLocal <SqlSession> (); // close sqlsession public static void closeSession () {SqlSession session = (SqlSession) threadLocal. get (); // 2 threadLocal. set (null); if (session! = Null) {session. close () ;}} public static SqlSession getSessionTwo () {// read the configuration file try {InputStream stream = Resources. getResourceAsStream (RESOURCE); sqlSessionFactory = new SqlSessionFactoryBuilder (). build (stream); return sqlSessionFactory. openSession ();} catch (IOException e) {e. printStackTrace ();} return null ;}}
V. Interface Layer
Public interface studentDao {/*** query all students * @ return */public List <student> getAllStudent (); /*** check the student id ** @ param * @ return */public student getStudentById (int id ); /*** add student ** @ return */public int addStudent (student stu ); /*** Delete student id * @ param id * @ return */public int deleStudent (int id );}
Vi. Testing
@ Test public void TestDele () throws IOException {// find the large configuration // String path = "mybatis-config.xml"; // read the configuration file // InputStream is = Resources. getResourceAsStream (path); // get the sqlsession factory // SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder (). build (is); // SqlSession session = sqlSessionFactory. openSession (); studentDao dao = MyBatis. getSessionTwo (). getMapper (studentDao. class); int value = da O. deleStudent (1); ------ modify ??? Below System. out. print (value );}
In addition, if sqlSession is not submitted during deletion and addition, the execution result will not affect the database and involve the Sqlsession thread,
The sqlsession of the execution method must be consistent with the sqlsession submitted by the execution.
SqlSession sessionTwo = MyBatis.getSessionTwo(); studentDao dao =sessionTwo.getMapper(studentDao.class); int id = dao.deleStudent(1); System.out.print(id); sessionTwo.commit();