JAVAEE - Mybatis Day 2: Input and Output Mapping, Dynamic sql, Association Query, Mybatis Integration spring, Mybatis Reverse Engineering

Keywords: Java Mybatis Spring xml SQL

1. Learning plan

1. Input Mapping and Output Mapping

a) Input parameter mapping

b) Return value mapping

2. Dynamic sql

a) If tag

b) Where label

c) Sql fragment

d) Foreach tag

3. Association Query

a) One-to-one Association

b) One-to-many Association

4. Mybatis Integration spring

a) How to integrate spring

b) Developing dao in a primitive way

c) Using Mapper interface dynamic proxy

5. Mybatis Reverse Engineering (Mastery)

2. Input and output mappings

The mapping file of Mapper.xml defines sql to operate database. Each sql is a statement, and the mapping file is the core of mybatis.

2.1. Environmental preparation

1. Reproduce yesterday's project and proceed according to the following figure

 

 

2. Paste and rename the following image

 

 

3. Keep only the files related to Mapper interface development, and delete the others.

The final effect is as follows:

 

 

4. Modify the SqlMapConfig.xml configuration file as shown below. Mapper mapper keeps only the way packages are scanned

 

 

2.2. parameterType (input type)

2.2.1. Passing Simple Types

Refer to the first day.

Use #{} placeholders, or ${} for sql splicing.

 

2.2.2. Passing pojo objects

Refer to the first day.

Mybatis uses an ognl expression to parse the value of an object field, and the value in parentheses of {} or ${} is the name of a pojo attribute.

 

2.2.3. Passing pojo wrapper objects

In development, query conditions can be passed by using pojo.

Query conditions may be comprehensive query conditions, including not only user query conditions but also other query conditions (for example, when querying user information, the user purchases commodity information is also used as query conditions). At this time, the input parameters can be transmitted using the packaging object.

Wrapping object: One property in the Pojo class is another pojo.

 

Requirement: User information is queried vaguely according to the user name, and the query condition is placed in the user attribute of QueryVo.

 

2.2.3.1. Write QueryVo

public class QueryVo {
    // Contains other pojo
    private User user;

    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
}

 

 

2.2.3.2. Sql statement

SELECT * FROM user WHERE username LIKE'% Zhang%'

2.2.3.3. Mapper.xml file

Configure sql in UserMapper.xml, as shown below.

 

 

2.2.3.4. Mapper interface

Add a method to the UserMapper interface as follows:

 

 

2.2.3.5. Test methods

Add test methods to UserMapeprTest as follows:

@Test
public void testQueryUserByQueryVo() {
    // mybatis and spring Integration, after integration, to spring Administration
    SqlSession sqlSession = this.sqlSessionFactory.openSession();
    // Establish Mapper The dynamic proxy object of the interface, after integration, is handed over to spring Administration
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

    // Use userMapper Execute queries using wrapper objects
    QueryVo queryVo = new QueryVo();
    // Set up user condition
    User user = new User();
    user.setUsername("Zhang");
    // Set to the wrapper object
    queryVo.setUser(user);

    // Execution query
    List<User> list = userMapper.queryUserByQueryVo(queryVo);
    for (User u : list) {
        System.out.println(u);
    }

    // mybatis and spring Integration, after integration, to spring Administration
    sqlSession.close();
}

 

 

2.2.3.6. Effect

The test results are as follows:

 

 

 

 

 

2.3. resultType (output type)

2.3.1. Output simple type

Requirements: Query the number of user table data bars

 

sql: SELECT count(*) FROM `user`

 

2.3.1.1. Mapper.xml file

Configure sql in UserMapper.xml as follows:

 

 

 

2.3.1.2. Mapper interface

Add a method to UserMapper as follows:

 

 

 

2.3.1.3. Test methods

Add test methods to UserMapeprTest as follows:

@Test
public void testQueryUserCount() {
    // mybatis and spring Integration, after integration, to spring Administration
    SqlSession sqlSession = this.sqlSessionFactory.openSession();
    // Establish Mapper The dynamic proxy object of the interface, after integration, is handed over to spring Administration
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

    // Use userMapper Number of query user data bars
    int count = userMapper.queryUserCount();
    System.out.println(count);

    // mybatis and spring Integration, after integration, to spring Administration
    sqlSession.close();
}

 

2.3.1.4. Effect

The test results are as follows:

 

 

Note: The result set that the output simple type must query has a record that eventually converts the value of the first field to the output type.

 

2.3.2. Output pojo objects

Reference to Day 1

2.3.3. Output pojo list

Refer to the first day.

2.4. resultMap

resultType can specify that the result of the query is mapped to pojo, but the mapping can be successful only if the attribute name of POJO and the column name of sql query are consistent.

If the sql query field name is inconsistent with the attribute name of pojo, the field name and the attribute name can be mapped as a corresponding relationship through resultMap, which essentially also needs to map the query result to the POJO object.

resultMap can map query results to complex types of pojo, such as one-to-one query and one-to-many query by including POJO and list in the query result mapping object.

 

Requirements: Query all the data in the order form

sql: SELECT id, user_id, number, createtime, note FROM `order`

2.4.1. Declare pojo objects

The database table is as follows:

 

 

 

Order object:

public class Order {
    // Order id
    private int id;
    // user id
    private Integer userId;
    // Order number
    private String number;
    // Order creation time
    private Date createtime;
    // Remarks
    private String note;
get/set. . . 
}

 

 

2.4.2. Mapper.xml file

Create the OrderMapper.xml configuration file as follows:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace: Namespaces for isolation sql,There is also a very important role. Mapper When using dynamic agent development, you need to specify Mapper Class path -->
<mapper namespace="cn.itcast.mybatis.mapper.OrderMapper">
    <!-- Query all order data -->
    <select id="queryOrderAll" resultType="order">
        SELECT id, user_id,
        number,
        createtime, note FROM `order`
    </select>
</mapper>

 

2.4.3. Mapper interface

Write the interface as follows:

 

public interface OrderMapper {
    /**
     * Query all orders
     * 
     * @return
     */
    List<Order> queryOrderAll();
}public interface OrderMapper {
    /**
     * Query all orders
     * 
     * @return
     */
    List<Order> queryOrderAll();
}

 

2.4.4. Test methods

Write the test method OrderMapperTest as follows:

public class OrderMapperTest {
    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void init() throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        this.sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Test
    public void testQueryAll() {
        // Obtain sqlSession
        SqlSession sqlSession = this.sqlSessionFactory.openSession();
        // Obtain OrderMapper
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);

        // Execution query
        List<Order> list = orderMapper.queryOrderAll();
        for (Order order : list) {
            System.out.println(order);
        }
    }
}

 

 

2.4.5. Effect

The test results are as follows:

 

 

It was found that userId was null

Solution: Using resultMap

 

2.4.6. Use resultMap

Because the sql query column (user_id) and the Order class attribute (userId) in mapper.xml above are inconsistent, the query results cannot be mapped to pojo.

resultMap needs to be defined to correspond the sql query column (user_id) to the Order class attribute (userId).

 

Modify OrderMapper.xml as follows:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace: Namespaces for isolation sql,There is also a very important role. Mapper When using dynamic agent development, you need to specify Mapper Class path -->
<mapper namespace="cn.itcast.mybatis.mapper.OrderMapper">

    <!-- resultMap Ultimately, the result is mapped to pojo Up, type That is to specify which one to map to. pojo -->
    <!-- id: Set up ResultMap Of id -->
    <resultMap type="order" id="orderResultMap">
        <!-- Primary key ,It's very important. If it is multiple fields,Define multiple id -->
        <!-- property: Primary key pojo Property name in -->
        <!-- column: Column names of primary keys in Databases -->
        <id property="id" column="id" />

        <!-- Define common attributes -->
        <result property="userId" column="user_id" />
        <result property="number" column="number" />
        <result property="createtime" column="createtime" />
        <result property="note" column="note" />
    </resultMap>

    <!-- Query all order data -->
    <select id="queryOrderAll" resultMap="orderResultMap">
        SELECT id, user_id,
        number,
        createtime, note FROM `order`
    </select>

</mapper>

 

 

2.4.7. Effect

Simply modify Mapper.xml, and test the results again as follows:

 

 

 

3. Dynamic sql

Dynamic splicing sql is realized by various tag methods provided by mybatis.

 

Requirements: Query users by gender and name

Query sql:

SELECT id, username, birthday, sex, address FROM `user` WHERE sex = 1 AND username LIKE'% Zhang%'

3.1. If Tags

Mapper.xml file

UserMapper.xml configures sql as follows:

 

<!-- Query users according to conditions -->
<select id="queryUserByWhere" parameterType="user" resultType="user">
    SELECT id, username, birthday, sex, address FROM `user`
    WHERE sex = #{sex} AND username LIKE
    '%${username}%'
</select>

 

3.1.2. Mapper interface

Write the Mapper interface as follows:

 

 

 

3.1.3. Test methods

Add test methods to UserMapperTest as follows:

 

@Test
public void testQueryUserByWhere() {
    // mybatis and spring Integration, after integration, to spring Administration
    SqlSession sqlSession = this.sqlSessionFactory.openSession();
    // Establish Mapper The dynamic proxy object of the interface, after integration, is handed over to spring Administration
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

    // Use userMapper Perform conditional queries for users
    User user = new User();
    user.setSex("1");
    user.setUsername("Zhang");

    List<User> list = userMapper.queryUserByWhere(user);

    for (User u : list) {
        System.out.println(u);
    }

    // mybatis and spring Integration, after integration, to spring Administration
    sqlSession.close();
}

 

3.1.4. Effect

The test results are as follows:

 

 

 

If user.setSex("1") is commented out, the test results are as follows:

 

 

Test result 2 is obviously unreasonable.

According to what we have learned before, to solve this problem, we need to write multiple sql. The more query conditions, the more SQL we need to write. Obviously, this is unreliable.

 

Solution, using dynamic sql if tag

 

3.1.5. Use the if tag

Modify UserMapper.xml as follows:

<!-- Query users according to conditions -->
<select id="queryUserByWhere" parameterType="user" resultType="user">
    SELECT id, username, birthday, sex, address FROM `user`
    WHERE 1=1
    <if test="sex != null and sex != ''">
        AND sex = #{sex}
    </if>
    <if test="username != null and username != ''">
        AND username LIKE
        '%${username}%'
    </if>
</select>

 

 

Note that data of string type needs to be checked for null strings.

3.1.6. Effect

 

 

As shown in the figure above, test OK

 

3.2. Where Label

The sql above also has the statement where 1=1, which is troublesome.

where tag can be used for modification

 

Modify UserMapper.xml as follows

<!-- Query users according to conditions -->
<select id="queryUserByWhere" parameterType="user" resultType="user">
    SELECT id, username, birthday, sex, address FROM `user`
<!-- where Labels can be added automatically where,Simultaneous processing sql The first in the statement and Keyword -->
    <where>
        <if test="sex != null">
            AND sex = #{sex}
        </if>
        <if test="username != null and username != ''">
            AND username LIKE
            '%${username}%'
        </if>
    </where>
</select>

 

 

3.2.1. Effect

The test results are as follows:

 

 

 

3.3. Sql fragment

Sql can extract duplicate SQL and use include reference to achieve SQL reuse.

 

The id, username, birthday, sex, addres in the above example are extracted as sql fragments, as follows:

<!-- Query users according to conditions -->
<select id="queryUserByWhere" parameterType="user" resultType="user">
    <!-- SELECT id, username, birthday, sex, address FROM `user` -->
    <!-- Use include Label loading sql Fragments; refid yes sql fragment id -->
    SELECT <include refid="userFields" /> FROM `user`
    <!-- where Labels can be added automatically where Keyword, processing at the same time sql The first in the statement and Keyword -->
    <where>
        <if test="sex != null">
            AND sex = #{sex}
        </if>
        <if test="username != null and username != ''">
            AND username LIKE
            '%${username}%'
        </if>
    </where>
</select>

<!-- statement sql fragment -->
<sql id="userFields">
    id, username, birthday, sex, address
</sql>

 

 

If you want to use sql fragments of other Mapper.xml configurations, you can add the corresponding Mapper.xml namespace before the refid

For example, below

 

 

3.4. foreach tag

Passing an array or List to sql, mybatis uses foreach parsing, as follows:

 

Query user information based on multiple IDS

Query sql:

SELECT * FROM user WHERE id IN (1,10,24)

 

3.4.1. Revamping QueryVo

The following figure defines list attribute ids in pojo to store multiple user ids and adds getter/setter methods

 

 

 

Mapper.xml file

UserMapper.xml adds sql as follows:

<!-- according to ids Query users -->
<select id="queryUserByIds" parameterType="queryVo" resultType="user">
    SELECT * FROM `user`
    <where>
        <!-- foreach Label, traversal -->
        <!-- collection: The traversal set, here is QueryVo Of ids attribute -->
        <!-- item: Items traversed can be written casually, but with the following#{}Be consistent inside -->
        <!-- open: Added earlier sql fragment -->
        <!-- close: Added at the end sql fragment -->
        <!-- separator: Specifies the separator to be used between traversal elements -->
        <foreach collection="ids" item="item" open="id IN (" close=")"
            separator=",">
            #{item}
        </foreach>
    </where>
</select>

 

 

The test methods are as follows:

@Test
public void testQueryUserByIds() {
    // mybatis and spring Integration, after integration, to spring Administration
    SqlSession sqlSession = this.sqlSessionFactory.openSession();
    // Establish Mapper The dynamic proxy object of the interface, after integration, is handed over to spring Administration
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

    // Use userMapper Perform conditional queries for users
    QueryVo queryVo = new QueryVo();
    List<Integer> ids = new ArrayList<>();
    ids.add(1);
    ids.add(10);
    ids.add(24);
    queryVo.setIds(ids);

    List<User> list = userMapper.queryUserByIds(queryVo);

    for (User u : list) {
        System.out.println(u);
    }

    // mybatis and spring Integration, after integration, to spring Administration
    sqlSession.close();
}

 

3.4.3. Effect

The test results are as follows:

 

 

Other implementations:

 

The underlying principle: instead of being referenced, the user passed in is recreated to create a map that stores the values in the user in the map. If the input is an array, it's placed in the array, and if it's a List, it's placed in the list.

 

 

 

4. Association query

4.1. Commodity Order Data Model

 

 

 

4.2. One-to-one query

Requirement: Query all order information, and query the next order user information.

 

Note: Since an order information can only be placed by one person, the query user information is associated with one-to-one query from the query order information. If the user's order information is queried from the user's information, it is one-to-many query, because a user can place more than one order.

 

sql statement:

SELECT
    o.id,
    o.user_id userId,
    o.number,
    o.createtime,
    o.note,
    u.username,
    u.address
FROM
    `orders` o
LEFT JOIN `user` u ON o.user_id = u.id

 

 

Method 1: Use resultType

Modify the order pojo class with resultType, which includes order information and user information

When the object is returned, mybatis automatically injects user information into it.

4.2.1.1. Modification of pojo class

After the OrderUser class inherits the Order class, the OrderUser class includes all the fields of the Order class. It only needs to define the user's information fields, as follows:

 

 

4.2.1.2. Mapper.xml

Add sql to UserMapper.xml as follows

<!-- Query orders and include user data -->
<select id="queryOrderUser" resultType="orderUser">
    SELECT
    o.id,
    o.user_id
    userId,
    o.number,
    o.createtime,
    o.note,
    u.username,
    u.address
    FROM
    `order` o
    LEFT JOIN `user` u ON o.user_id = u.id
</select>

 

4.2.1.3. Mapper interface

Add a method to the UserMapper interface as follows:

 

 

4.2.1.4. Test methods:

Add test methods to UserMapperTest as follows:

@Test
public void testQueryOrderUser() {
    // mybatis and spring Integration, after integration, to spring Administration
    SqlSession sqlSession = this.sqlSessionFactory.openSession();
    // Establish Mapper The dynamic proxy object of the interface, after integration, is handed over to spring Administration
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

    // Use userMapper Perform conditional queries for users
    List<OrderUser> list = userMapper.queryOrderUser();

    for (OrderUser ou : list) {
        System.out.println(ou);
    }

    // mybatis and spring Integration, after integration, to spring Administration
    sqlSession.close();
}

 

4.2.1.5. Effect

The test results are as follows:

 

 

4.2.1.6. Summary

A special pojo class is defined as the output type, in which all fields of the sql query result set are defined. This method is relatively simple and widely used in enterprises.

 

Method 2: Use resultMap

Using resultMap, a special resultMap is defined to map one-to-one query results.

 

4.2.2.1. Modification of pojo class

The user attribute is added to the Order class to store the user information of the association query. Because the order Association query user is one-to-one, a single user object is used to store the user information of the association query.

Modify Order as follows:

 

 

 

4.2.2.2. Mapper.xml

Here resultMap specifies orderUserResultMap, as follows:

<resultMap type="order" id="orderUserResultMap">
    <id property="id" column="id" />
    <result property="userId" column="user_id" />
    <result property="number" column="number" />
    <result property="createtime" column="createtime" />
    <result property="note" column="note" />

    <!-- association : Configure one-to-one attributes -->
    <!-- property:order Inside User Attribute name -->
    <!-- javaType:Attribute types -->
    <association property="user" javaType="user">
        <!-- id:Declare the primary key to indicate user_id Is the unique identifier of the associated query object-->
        <id property="id" column="user_id" />
        <result property="username" column="username" />
        <result property="address" column="address" />
    </association>

</resultMap>

<!-- One-to-one associations, queries orders, orders containing user attributes -->
<select id="queryOrderUserResultMap" resultMap="orderUserResultMap">
    SELECT
    o.id,
    o.user_id,
    o.number,
    o.createtime,
    o.note,
    u.username,
    u.address
    FROM
    `order` o
    LEFT JOIN `user` u ON o.user_id = u.id
</select>

 

4.2.2.3. Mapper interface

Write User Mapper as follows:

 

 

 

4.2.2.4. Test methods

Add test methods to UserMapperTest as follows:

@Test
public void testQueryOrderUserResultMap() {
    // mybatis and spring Integration, after integration, to spring Administration
    SqlSession sqlSession = this.sqlSessionFactory.openSession();
    // Establish Mapper The dynamic proxy object of the interface, after integration, is handed over to spring Administration
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

    // Use userMapper Perform conditional queries for users
    List<Order> list = userMapper.queryOrderUserResultMap();

    for (Order o : list) {
        System.out.println(o);
    }

    // mybatis and spring Integration, after integration, to spring Administration
    sqlSession.close();
}

 

4.2.2.5. Effect

The test results are as follows:

 

 

 

4.3. One-to-many queries

Case: Query all user information and user-related order information.

User information and order information are one-to-many relationships.

 

sql statement:

SELECT
    u.id,
    u.username,
    u.birthday,
    u.sex,
    u.address,
    o.id oid,
    o.number,
    o.createtime,
    o.note
FROM
    `user` u
LEFT JOIN `order` o ON u.id = o.user_id

 

4.3.1. Modify the pojo class

Add the List < Order > orders attribute to the User class as follows:

 

 

 

4.3.2. Mapper.xml

Add sql to UserMapper.xml as follows:

<resultMap type="user" id="userOrderResultMap">
    <id property="id" column="id" />
    <result property="username" column="username" />
    <result property="birthday" column="birthday" />
    <result property="sex" column="sex" />
    <result property="address" column="address" />

    <!-- Configure one-to-many relationships -->
    <collection property="orders" javaType="list" ofType="order">
        <!-- The key to configuration is association. Order Unique ID -->
        <id property="id" column="oid" />
        <result property="number" column="number" />
        <result property="createtime" column="createtime" />
        <result property="note" column="note" />
    </collection>
</resultMap>

<!-- One-to-many association, querying orders and querying orders placed by the user -->
<select id="queryUserOrder" resultMap="userOrderResultMap">
    SELECT
    u.id,
    u.username,
    u.birthday,
    u.sex,
    u.address,
    o.id oid,
    o.number,
    o.createtime,
    o.note
    FROM
    `user` u
    LEFT JOIN `order` o ON u.id = o.user_id
</select>

 

4.3.3. Mapper interface

Write the UserMapper interface as follows:

 

 

 

4.3.4. Test methods

Add test methods to UserMapperTest as follows

@Test
public void testQueryUserOrder() {
    // mybatis and spring Integration, after integration, to spring Administration
    SqlSession sqlSession = this.sqlSessionFactory.openSession();
    // Establish Mapper The dynamic proxy object of the interface, after integration, is handed over to spring Administration
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

    // Use userMapper Perform conditional queries for users
    List<User> list = userMapper.queryUserOrder();

    for (User u : list) {
        System.out.println(u);
    }

    // mybatis and spring Integration, after integration, to spring Administration
    sqlSession.close();
}

 

4.3.5. Effect

The test results are as follows:

 

 

 

 

 

5. Mybatis integrates spring

5.1. Integration

1. The SqlSessionFactory object should be placed in the spring container as a singleton.

2. In the traditional development of dao, sqlsession objects should be obtained from spring container.

3. In the form of Mapper proxy, the proxy object of mapper should be obtained directly from the spring container.

4. Connection of database and transaction management of database connection pool are handed over to spring container.

5.2. Integrating jar packages needed

1. spring's jar package

2. Mybatis jar package

3. Spring+mybatis package.

4. Mysql's database-driven jar package.

5. The jar package of the database connection pool.

The location of the jar package is as follows:

 

5.3. Steps for integration

5.3.1. Creating Engineering

Create a java project as follows:

 

 

5.3.2. Import jar packages

The jar package mentioned earlier needs to be imported as follows:

 

 

 

5.3.3. Add configuration files

  1. Configuration file for mybatisSpring
  2. The configuration file sqlmapConfig.xml

a) Database connection and connection pool

b) Transaction management (temporarily not configurable)

c) sqlsessionFactory object, configured in spring container

d) The mapeer proxy object or dao implementation class is configured in the spring container.

 

Create a copy of the resource folder config to add the configuration file, as shown below

 

 

 

5.3.3.1. SqlMapConfig.xml

The configuration file is SqlMapConfig.xml, as follows:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- Setting aliases -->
    <typeAliases>
        <!-- 2. Specifying a scan package will alias all classes in the package. The name of the alias is the class name. Case insensitive -->
        <package name="cn.itcast.mybatis.pojo" />
    </typeAliases>

</configuration>

 

 

5.3.3.2. applicationContext.xml

SqlSession FactoryBean belongs to the jar package mybatis-spring

mybatis is another architecture for spring, requiring the integration of jar packages.

 

Add mybatis-spring-1.2.2.jar source code to the project, as shown below

 

 

 

 

As shown in the figure below, the icon changes to indicate that the source code was loaded successfully:

 

 

Integrating Mybatis requires SqlSession FactoryBean, located in the following figure:

 

 

 

applicationContext.xml, which is configured as follows

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

   <!-- Load configuration file -->
   <context:property-placeholder location="classpath:db.properties" />

    <!-- Database connection pool -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="10" />
        <property name="maxIdle" value="5" />
    </bean>

    <!-- To configure SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- To configure mybatis Core Profile -->
        <property name="configLocation" value="classpath:SqlMapConfig.xml" />
        <!-- Configuring data sources -->
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>

 

5.3.3.3. db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

5.3.3.4. log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

5.3.3.5. Effect:

The final effect of the added configuration file is as follows:

 

 

5.4. Development of Dao

Two ways to implement dao:

1. Development of Original dao

2. Using Mapper Agent Form to Develop

a) Configure Mapper proxy directly

b) Configuring Mapper Agents with Scan Packets

 

Demand:

  1. Implementing queries based on user id
  2. Implementing Fuzzy Query Based on User Name
  3. Add user

 

5.4.1. Create pojo

public class User {
    private int id;
    private String username;// User name
    private String sex;// Gender
    private Date birthday;// Birthday
    private String address;// address

get/set. . . 
}

 

 

5.4.2. Development of traditional dao

The original DAO development interface + implementation class to complete.

Need dao implementation class needs to inherit SqlsessionDaoSupport class

 

5.4.2.1. Implementing Mapper.xml

Write the User.xml configuration file as follows:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="test">
    <!-- According to users id query -->
    <select id="queryUserById" parameterType="int" resultType="user">
        select * from user where id = #{id}
    </select>

    <!-- Fuzzy Query User Based on User Name -->
    <select id="queryUserByUsername" parameterType="string"
        resultType="user">
        select * from user where username like '%${value}%'
    </select>

    <!-- Add user -->
    <insert id="saveUser" parameterType="user">
        <selectKey keyProperty="id" keyColumn="id" order="AFTER"
            resultType="int">
            select last_insert_id()
        </selectKey>
        insert into user
        (username,birthday,sex,address)
        values
        (#{username},#{birthday},#{sex},#{address})
    </insert>

</mapper>

 

5.4.2.2. Load Mapper.xml

The SqlMapConfig is configured as follows:

 

 

 

5.4.2.3. Implementing UserDao interface

public interface UserDao {
    /**
     * Query users according to id
     * 
     * @param id
     * @return
     */
    User queryUserById(int id);

    /**
     * Fuzzy Query User List Based on User Name
     * 
     * @param username
     * @return
     */
    List<User> queryUserByUsername(String username);

    /**
     * Preservation
     * 
     * @param user
     */
    void saveUser(User user);

}

 

 

 

5.4.2.4. Implementing the UserDaoImpl implementation class

Writing DAO implementation classes, implementation classes must integrate SqlSession DaoSupport

SqlSession DaoSupport provides getSqlSession() method to obtain SqlSession

public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
    @Override
    public User queryUserById(int id) {
        // Obtain SqlSession
        SqlSession sqlSession = super.getSqlSession();

        // Use SqlSession Execution operation
        User user = sqlSession.selectOne("queryUserById", id);

        // Do not shut down. sqlSession

        return user;
    }

    @Override
    public List<User> queryUserByUsername(String username) {
        // Obtain SqlSession
        SqlSession sqlSession = super.getSqlSession();

        // Use SqlSession Execution operation
        List<User> list = sqlSession.selectList("queryUserByUsername", username);

        // Do not shut down. sqlSession

        return list;
    }

    @Override
    public void saveUser(User user) {
        // Obtain SqlSession
        SqlSession sqlSession = super.getSqlSession();

        // Use SqlSession Execution operation
        sqlSession.insert("saveUser", user);

        // No need to submit,Transaction by spring Conduct management
        // Do not shut down. sqlSession
    }
}

 

 

5.4.2.5. Configure dao

Configure the dao implementation class into the spring container, as shown below

 

 

 

5.4.2.6. Test methods

Creating test methods allows you to create test Junit use cases directly.

Create as shown in the figure below.

 

 

The test methods are as follows:

 

public class UserDaoTest {
    private ApplicationContext context;

    @Before
    public void setUp() throws Exception {
        this.context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    }

    @Test
    public void testQueryUserById() {
        // Obtain userDao
        UserDao userDao = this.context.getBean(UserDao.class);

        User user = userDao.queryUserById(1);
        System.out.println(user);
    }

    @Test
    public void testQueryUserByUsername() {
        // Obtain userDao
        UserDao userDao = this.context.getBean(UserDao.class);

        List<User> list = userDao.queryUserByUsername("Zhang");
        for (User user : list) {
            System.out.println(user);
        }
    }

    @Test
    public void testSaveUser() {
        // Obtain userDao
        UserDao userDao = this.context.getBean(UserDao.class);

        User user = new User();
        user.setUsername("Cao Cao");
        user.setSex("1");
        user.setBirthday(new Date());
        user.setAddress("Three countries");
        userDao.saveUser(user);
        System.out.println(user);
    }
}

 

5.4.3. Mapper Agent Form Development dao

5.4.3.1. Implementing Mapper.xml

Write the UserMapper.xml configuration file as follows:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.itcast.mybatis.mapper.UserMapper">
    <!-- According to users id query -->
    <select id="queryUserById" parameterType="int" resultType="user">
        select * from user where id = #{id}
    </select>

    <!-- Fuzzy Query User Based on User Name -->
    <select id="queryUserByUsername" parameterType="string"
        resultType="user">
        select * from user where username like '%${value}%'
    </select>

    <!-- Add user -->
    <insert id="saveUser" parameterType="user">
        <selectKey keyProperty="id" keyColumn="id" order="AFTER"
            resultType="int">
            select last_insert_id()
        </selectKey>
        insert into user
        (username,birthday,sex,address) values
        (#{username},#{birthday},#{sex},#{address})
    </insert>
</mapper>

 

5.4.3.2. Implementing UserMapper interface

public interface UserMapper {
    /**
     * Query according to user id
     * 
     * @param id
     * @return
     */
    User queryUserById(int id);

    /**
     * Fuzzy Query User Based on User Name
     * 
     * @param username
     * @return
     */
    List<User> queryUserByUsername(String username);

    /**
     * Add user
     * 
     * @param user
     */
    void saveUser(User user);
}

 

5.4.3.3 Mode 1: Configure mapper agent

Add configuration in applicationContext.xml

MapperFactoryBean is also part of the mybatis-spring integration package

<! - Mapper agent development mode 1, configure Mapper agent object - > 2.
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
    <! - Configure Mapper interface - >
    <property name="mapperInterface" value="cn.itcast.mybatis.mapper.UserMapper" />
    <! - Configure sqlSessionFactory - >
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

 

5.4.3.4. Test methods

public class UserMapperTest {
    private ApplicationContext context;

    @Before
    public void setUp() throws Exception {
        this.context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    }

    @Test
    public void testQueryUserById() {
        // Obtain Mapper
        UserMapper userMapper = this.context.getBean(UserMapper.class);

        User user = userMapper.queryUserById(1);
        System.out.println(user);
    }

    @Test
    public void testQueryUserByUsername() {
        // Obtain Mapper
        UserMapper userMapper = this.context.getBean(UserMapper.class);

        List<User> list = userMapper.queryUserByUsername("Zhang");

        for (User user : list) {
            System.out.println(user);
        }
    }
    @Test
    public void testSaveUser() {
        // Obtain Mapper
        UserMapper userMapper = this.context.getBean(UserMapper.class);

        User user = new User();
        user.setUsername("Cao Cao");
        user.setSex("1");
        user.setBirthday(new Date());
        user.setAddress("Three countries");

        userMapper.saveUser(user);
        System.out.println(user);
    }
}

 

 

5.4.3.5 Mode 2: Scanning package configuration mapper

<! - Mapper Agent Development Mode II, Scanning Packet Configuration Agent - >
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <! - Configure Mapper interface - >
    <property name="basePackage" value="cn.itcast.mybatis.mapper" />
</bean>

 

The id of each mapper proxy object is the class name, initially lowercase

6. Mybatis reverse engineering

Use the Mapper automatic generation tool mybatis-generator-core-1.3.2 of the official website to generate po classes and Mapper mapping files

6.1. Importing Reverse Engineering

Reverse engineering has been done using pre-class materials, as follows:

 

 

 

6.1.1. Copy reverse engineering into the workspace

The effect of replication is as follows:

 

 

 

6.1.2. Import reverse engineering into eclipse

Import as follows:

 

 

 

6.2. Modifying configuration files

Configure the details generated by Mapper in generatorConfig.xml as follows:

 

 

Attention should be paid to modifying the following points:

  1. Modify the database tables to be generated
  2. The package path where the pojo file resides
  3. Mapper's Packet Path

 

The configuration file is as follows:

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <context id="testTables" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- Whether to remove automatically generated comments true: Yes, false:no -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--Information about database connection: driver class, connection address, user name, password -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password="root">
        </jdbcConnection>
        <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver" connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg" 
            userId="yycg" password="yycg"> </jdbcConnection> -->

        <!-- default false,hold JDBC DECIMAL and NUMERIC Type resolution is Integer,by true Shi Ba JDBC DECIMAL 
            //And NUMERIC types resolved to java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- targetProject:generate PO Class location -->
        <javaModelGenerator targetPackage="cn.itcast.ssm.po"
            targetProject=".\src">
            <!-- enableSubPackages:Whether to let schema Suffixes for packages -->
            <property name="enableSubPackages" value="false" />
            <!-- Spaces before and after values returned from the database are cleaned up -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- targetProject:mapper Location of mapping file generation -->
        <sqlMapGenerator targetPackage="cn.itcast.ssm.mapper"
            targetProject=".\src">
            <!-- enableSubPackages:Whether to let schema Suffixes for packages -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- targetPackage: mapper Location of interface generation -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="cn.itcast.ssm.mapper" targetProject=".\src">
            <!-- enableSubPackages:Whether to let schema Suffixes for packages -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <!-- Specify database tables -->
        <table schema="" tableName="user"></table>
        <table schema="" tableName="order"></table>
    </context>
</generatorConfiguration>

 

6.3. Generating reverse engineering code

Find the java file shown below and execute the main function of the project.

 

 

 

 

Refresh the project and discover code generation as follows:

 

 

6.4. Testing reverse engineering code

1. Copy the generated code to the mybatis-spring project, as shown below

 

 

 

2. Modify spring configuration file

Modify in applicationContext.xml

 

<! - Mapper Agent Development, Scanning Packet Configuration Agent - >
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <! - Configure Mapper interface, if you need to load more than one package, write it in directly, use it in the middle, and separate it - >.
    <!-- <property name="basePackage" value="cn.itcast.mybatis.mapper" /> -->
    <property name="basePackage" value="cn.itcast.ssm.mapper" />
</bean>

 

 

3. Write test methods:

 

public class UserMapperTest {
    private ApplicationContext context;

    @Before
    public void setUp() throws Exception {
        this.context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    }

    @Test
    public void testInsert() {
        // Obtain Mapper
        UserMapper userMapper = this.context.getBean(UserMapper.class);

        User user = new User();
        user.setUsername("Cao Cao");
        user.setSex("1");
        user.setBirthday(new Date());
        user.setAddress("Three countries");

        userMapper.insert(user);
    }

    @Test
    public void testSelectByExample() {
        // Obtain Mapper
        UserMapper userMapper = this.context.getBean(UserMapper.class);

        // Establish User Object Extension Class, User Sets Query Conditions
        UserExample example = new UserExample();
        example.createCriteria().andUsernameLike("%Zhang%");

        // Query data
        List<User> list = userMapper.selectByExample(example);

        System.out.println(list.size());
    }

    @Test
    public void testSelectByPrimaryKey() {
        // Obtain Mapper
        UserMapper userMapper = this.context.getBean(UserMapper.class);

        User user = userMapper.selectByPrimaryKey(1);
        System.out.println(user);
    }
}

 

Be careful:

    1. The code generated by reverse engineering can only do single table queries
    2. You can't extend the generated code because if the database changes, you need to reuse reverse engineering to generate the code, and the original code is overwritten.
    3. A table generates four files

Posted by rodin69 on Fri, 14 Dec 2018 11:00:04 -0800