Good Java programmer learning basic route shared use of MyBatis

  Good programmers Java learning routes Share MyBatis basic use, the Introduction, this chapter we will begin to learn a very good ORM (object-relational mapping) framework: MyBatis , it is currently the most used enterprise database framework.

MyBatis Profile

       MyBatis is an excellent persistence framework that supports custom SQL , stored procedures and advanced mappings. MyBatis avoids almost all the JDBC code and manual setting parameters and obtaining the result set. MyBatis can use simple XML or annotations configuration and mapping primitive types, interfaces, and Java in POJO ( Plain Old Java Objects , plain old Java objects) for the records in the database.

  MyBatis advantages are:

  1 ) instead of JDBC completion of the CRUD , high efficiency

  2 ) easy to use, flexible configuration

  3 ) The company is currently the mainstream ORM (object-relational mapping) framework

 

MyBatis basic configuration

  Here we use Maven to build the project,

  First need to import MyBatis and MySQL driven dependencies

  Then add profiles, MyBatis configuration file is divided into two types:

  1 , MyBatis framework of the overall profile

  2 , MyBatis database mapping file

  

MyBatis configuration file

  We can project resources to add profile directory: the mybatis-config.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  4. <configuration>
  5. <-! environments representative of the environment configuration set, this label can have a plurality of Environment ,
  6. default attribute is selected as the default environment wherein a configuration environment ->
  7. <environments default="develop">
  8. <environment id="develop">
  9. <! - here to configure the transaction manager for the JDBC type ->
  10. <transactionManager type="JDBC"/>
  11. <! - here configure the data source, the POOLED Representative connection pool of data sources ->
  12. <dataSource type="POOLED">
  13. <! - This is commonly used 4 database configuration: drive, the URL of , account numbers, passwords ->
  14. <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
  15. <property name="url" value="jdbc:mysql://localhost/java1903?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8"/>
  16. <property name="username" value="root"/>
  17. <property name="password" value="123456"/>
  18. </dataSource>
  19. </environment>
  20. </environments>
  21. <! - This is the path to the configuration database mapping file, if the file is configured to have a map here ->
  22. <mappers>
  23. <mapper resource="mappers/userMapper.xml"/>
  24. </mappers>
  25. </configuration>

 

 

Mapping file

  MyBatis can be realized by Java to manipulate objects in the database, then how Java to query and modify the operation of the database table objects it? MyBatis to configure the data mapping files by SQL operations, the Java mapping method is the object of a specific SQL statement.

  Here, we first write a UserDAO Interface

  1. package com.qianfeng.mybatis.dao;
  2. public interface UserDAO
  3. {
  4. List<User> selectAll();
  5. void insert(User user);
  6. }

  Next we come to realize the configuration file mapping User lookup table and insert:

  

  In the resources created under mappers directory, then create a map file: userMapper.xml

  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. <-! Namespace corresponding to the configuration of the Java Interface ->
  6. <mapper namespace="com.qianfeng.mybatis.dao.UserDAO">
  7. ...
  8. </mapper>

  mapper can configure four labels:

  . 1 ) <insert> configuration insert statement

  2 ) <update> configuration update statement

  3 ) <delete> Configuration delete statement

  . 4 ) <select> configuration select statement

  Common attributes of these four labels are:

. 1)   ID   corresponding to DAO method name interface

  2 ) the parameterType    parameters corresponding to the type of method

  3returnType   

   The method returns the value corresponding to the type, such as: the User (name attribute field names and the same type of table)

4)  returnMap       

The method returns a value corresponding to the type of mapping for the property name and the field name of the class is not the same table, the mapping between field names and to achieve properties

  

  Here insert configuration example

  1. <insert id="insert" parameterType="com.qianfeng.mybatis.entity.User">
  2. insert into tb_user(u_name,u_password,u_realname,u_gender,u_age,u_img)
  3. values(#{name},#{password},#{realname},#{gender},#{age},#{img})
  4. </insert>

  Wherein # {...} contained in the User attribute name object

 

  select configuration example

  1. <resultMap id="userMap" type="com.qianfeng.mybatis.entity.User">
  2. <id property="id" column="u_id"></id>
  3. <result property="name" column="u_name"></result>
  4. <result property="password" column="u_password"></result>
  5. <result property="realname" column="u_realname"></result>
  6. <result property="gender" column="u_gender"></result>
  7. <result property="age" column="u_age"></result>
  8. <result property="img" column="u_img"></result>
  9. </resultMap>
  10. <select id="selectAll" resultMap="userMap">
  11. select * from tb_user
  12. </select>

  returnMap configuration query returned map objects and database tables, the above mentioned id is mapped name, of the type is the corresponding Java type

  Wherein the sub-label id is the primary key configuration table, the attribute id is Java attribute class name, column is the field name of the table, there javaType and jdbcType attributes can be configured Java type and Jdbc data type, the two may be omitted herein.

  result is generally disposed in addition to the primary key column

 

MyBatis to use

The main steps:

  1. 1 ) reads the configuration file, create SqlSessionFactory
  2. SqlSessionFactory factory =
  3. new SqlSessionFactoryBuilder().build(
  4. Resources.getResourceAsStream("mybatis-config.xml"));
  5. 2 ) Open the database sessions Session
  6. SqlSession sqlSession = factory.openSession();
  7. 3 ) obtain DAO proxy object interface
  8. UserDAO mapper = sqlSession.getMapper(UserDAO.class);
  9. 4 ) perform CRUD
  10. mapper.insert(...);
  11. 5) commits the transaction
  12. sqlSession.commit();
  13. 6) close the session
  14. sqlSession.close();

to sum up

  In this chapter we learned the basic configuration of MyBatis, maps and written documents, but also mastered the basic method of operation of MyBatis, MyBatis there are many powerful features, we will learn later.

Guess you like

Origin www.cnblogs.com/gcghcxy/p/11447952.html
Recommended