<?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"/>
<!--开启二级缓存-->
<settings>
<setting name="cacheEnabled" value="true"/>
</settings>
<!--配置别名-->
<typeAliases>
<package name="com.hope.domain"/>
</typeAliases>
<!--配置环境-->
<environments default="mysql">
<environment id="mysql">
<!--配置事务-->
<transactionManager type="JDBC"/>
<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>
<package name="com.hope.dao"/>
</mappers>
</configuration>
package com.hope.dao;
import com.hope.domain.User;
import com.sun.xml.internal.bind.v2.model.core.ID;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import java.util.List;
/**
* @author newcityman
* @date 2019/11/15 - 22:59
* 使用二级缓存
*/
@CacheNamespace(blocking = true)
public interface IUserDao {
/**
* 查询所有用户的信息
* @return
*/
@Select(value = "select * from user")
@Results(id = "userMap",value = {
@Result(id = true,column ="id" ,property ="userId" ),
@Result(column = "username" ,property = "userName"),
@Result(column = "sex",property = "userSex"),
@Result(column = "address",property = "userAddress"),
@Result(column = "birthday",property = "userBirthday"),
@Result(column = "id",property = "accounts",
many =@Many(select = "com.hope.dao.IAccountDao.findAccountById"
,fetchType = FetchType.LAZY)),
})
public List<User> findAll();
/**
* 根据用户的id,查询用户
* @param id
* @return
*/
@Select("select * from user where id=#{id}")
@ResultMap(value ={"userMap"} )
User findOne(Integer id);
/**
* 根据用户名模糊查询
* @param username
* @return
*/
@Select("select * from user where username like #{username}")
@ResultMap("userMap")
List<User> findByName(String username);
}