SSM-Mybatis Learning Record (5) Annotation Development

Annotation development in Mybatis

1. Project structure
Insert picture description here
2. Environment construction (main configuration file SqlMapConfig.xml)

<?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>
<!--    映入外部配置文件-->
    <properties resource="jdbcConfig.properties"></properties>

<!--    配置开启二级缓存-->
    <settings>
<!--        默认也是开启的-->
        <setting name="cacheEnabled" value="true"/>
    </settings>
<!--    配置别名-->
    <typeAliases>
        <package name="com.ccsu.domain"/>
    </typeAliases>
<!--    配置环境-->
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
<!--    指定带有注解的dao接口的所在位置-->
    <mappers>
<!--        <mapper class="com.ccsu.dao.IUserDao"></mapper>-->
        <package name="com.ccsu.dao"/>
    </mappers>
</configuration>
	单表CRUD操作(代理Dao方式,省略查和插入)

Insert picture description here
3. Multi-table query operation

Create a mapping under the User table

Insert picture description here

IUserDao interface refers to the method under the IAccountDao interface

Insert picture description here

fetchType = FerchType.lazy is lazy loading

Insert picture description here
4. Cache configuration
Insert picture description here

Annotation on the interface name

Insert picture description here

The IAccountDao code is as follows (the main configuration file is on)

package com.ccsu.dao;

import com.ccsu.domain.Account;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;
/**
 * 账户的持久层
 */
public interface IAccountDao {

    /**
     * 查找所有账户,并且获取每个账户所属的用户信息
     * @return
     */
    @Results(id="accountMap",value={
            @Result(id = true,property = "aid",column = "id"),
            @Result(property = "uid",column = "uid"),
            @Result(property = "money",column = "money"),
            @Result(column = "uid",property = "user",one = @One(
                    select = "com.ccsu.dao.IUserDao.findById",fetchType = FetchType.EAGER))
    })
    @Select("select * from account")
    List<Account> findAllAccount();

    /**
     * 通过uid查询出该用户下的所有账户
     * @param uid
     * @return
     */
    @ResultMap(value = "accountMap")
    @Select("select * from account where uid=#{uid}")
    List<Account> findAccountByUid(Integer uid);
}

IUserDao code is as follows

package com.ccsu.dao;

import com.ccsu.domain.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * 用户持久层接口
 *      在mybatis中针对 CRUD一共又四个注解
 *      Select、Insert、Update、Delete
 */
@CacheNamespace(blocking = true)
public interface IUserDao {

    /**
     * 查询所有的用户,并且查询出该用户下的所有账户
     * @return
     */
    //如果实体类和数据库字段对应不上使用如下注解
    @Results(id = "userMap",value = {
            @Result(id=true,property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "address",column = "address"),
            @Result(property = "sex",column = "sex"),
            @Result(column = "id",property = "accounts",many = @Many(
                    select = "com.ccsu.dao.IAccountDao.findAccountByUid",
                    fetchType = FetchType.LAZY
            ))
    })
    @Select({"select * from user"})
    List<User> findAll();

    /**
     * 插入一个新用户
     * @param user
     */
    //当字段不符时,使用以下注解
    @ResultMap("userMap")
    @Insert({"insert into user(username,address,sex,birthday) values(#{username},#{address},#{sex},#{birthday})"})
    void insertUser(User user);

    /**
     * 更新用户
     * @param user
     */
    @Update("update user set username=#{username},address=#{address} where id = #{id}")
    void updateUser(User user);

    /**
     * 根据id删除用户
     */
    @Delete("delete from user where id=#{id}")
    void deleteUser(Integer id);

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    //当字段不符时,使用以下注解
    @ResultMap("userMap")
    @Select("select * from user where id = #{id} ")
    User findById(Integer id);

    /**
     * 模糊查询,根据名字
     * @return
     * @param
     */
    @Select("select * from user where username like #{username}")
    List<User> findByName(String username);

    /**
     * 查询总用户数量
     * @return
     */
    @Select("select count(*) from user")
    int findTotal();
}

Published 12 original articles · Liked3 · Visits 407

Guess you like

Origin blog.csdn.net/Hobo_hua/article/details/104026259