Mybatis学习(二)之配置文件

Mybatis学习(二)之配置文件

IAccountDao

package com.itheima.dao;

import com.itheima.domain.Account;
import com.itheima.domain.AccountUser;

import java.util.List;

/**
 * @author :班奈
 * @version 1.0
 * @date 2021/1/20 10:23
 */
public interface IAccountDao {
     
    /**
     * select * from account a,user u where u.uid=a.uid;
     * select u.*,a.aid,a.uid,a.money,a.aname from account a,user u where u.uid=a.uid;
     *
     *
     * 查询所有账户,同时还要获取到当前账户所属用户的信息
     * @return
     */

        List<Account> findAll();
        List<AccountUser> findAllAccount();
}

IRoleDao

package com.itheima.dao;

import com.itheima.domain.Role;

import java.util.List;

/**
 * @author :班奈
 * @version 1.0
 * @date 2021/1/21 16:41
 */
public interface IRoleDao {
     
    /**
     * 查询所有
     */
    List<Role> findAll();

    /**
     * 查询角色,同时获取角色所赋予的用户
     * select * from role r left outer join user_role ur on r.rid=ur.rid ;
     * select * from role r left outer join user_role ur on r.rid=ur.rid left outer join user u on u.uid=ur.uid;
     * select u.*,r.rid,r.role_name,r.role_desc from role r left outer join user_role ur on r.rid=ur.rid left outer join user u on u.uid=ur.uid;
     *
     */
}

IUserDao

package com.itheima.dao;
import com.itheima.domain.User;
import java.util.List;

/*
   用户i持久层接口
    */
public interface IUserDao {
     
    /**
     * 查询所有操作
     * @return
     */
    List<User> findAll();
    /***
     * select u.*,r.rid,r.role_name,r.role_desc from user u left outer join user_role ur on u.uid=ur.uid left outer join role r on r.rid=ur.rid;
     */
    List<User> findAllUserToRole();
}

IAccountDao.xml

<?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="com.itheima.dao.IAccountDao">
    <!-- 定义封装account和user的resultMap-->
    <resultMap id="accountUserMap" type="com.itheima.domain.Account">
        <id property="aid" column="aid"></id>
        <result property="uid" column="uid"></result>
        <result property="money" column="money"></result>
        <result property="aname" column="aname"></result>
        <!--一对一的关系映射,配置封装user的内容-->
        <association property="user" column="uid" javaType="com.itheima.domain.User"><!--cloum告诉mybatis通过那个字段来获取user对象的内容-->
            <id property="uid" column="uid"></id>
            <result column="username" property="username"></result>
            <result column="address" property="address"></result>
            <result column="sex" property="sex"></result>
            <result column="birthday" property="birthday"></result>
        </association>
    </resultMap>
    <!-- p配置查询所有-->
    <!-- id不能随便写要写改Dao的方法名  resultType将结果集封装到user对象中-->
    <select id="findAll" resultMap="accountUserMap">
            select u.*,a.aid,a.uid,a.money,a.aname from account a,user u where u.uid=a.uid;
        </select>
   <!-- 查询所有同时包含用户名和地址信息-->
    <select id="findAllAccount"  resultType="com.itheima.domain.AccountUser">
         /* select u.*,a.aid,a.uid,a.money,a.aname from account a,user u where u.uid=a.uid;*/
         select a.*,u.username,u.address from account a,user u where u.uid=a.uid;
    </select>
</mapper>

IRoleDao.xml

<?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="com.itheima.dao.IRoleDao">
       <!-- 定义role表的resultMap-->
        <resultMap id="roleMap" type="com.itheima.domain.Role">
            <id property="rid" column="rid"></id>
            <result property="roleName" column="role_name"></result>
            <result property="roleDesc" column="role_desc"></result>
            <collection property="users" ofType="com.itheima.domain.User">
                <id column="uid" property="uid"></id>
                <result column="username" property="username"></result>
                <result column="address" property="address"></result>
                <result column="sex" property="sex"></result>
                <result column="birthday" property="birthday"></result>
            </collection>
        </resultMap>
        <!--查询所有-->
        <select id="findAll" resultMap="roleMap">
            <!--换行回车后加空格-->
            select u.*,r.rid,r.role_name,r.role_desc from role r
             left outer join user_role ur on r.rid=ur.rid
             left outer join user u on u.uid=ur.uid;
        </select>
</mapper>

IUserDao.xml

<?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="com.itheima.dao.IUserDao">
    <!--定义User的resultMap-->
    <resultMap id="userAccountMap" type="com.itheima.domain.User">
        <id property="uid" column="uid"></id>
        <result property="username" column="username"></result>
        <result property="address" column="address"></result>
        <result property="sex" column="sex"></result>
        <result property="birthday" column="birthday"></result>
        <collection property="accounts" ofType="com.itheima.domain.Account">
            <id column="aid" property="aid"></id>
            <result column="uid" property="uid"></result>
            <result property="money" column="money"></result>
            <result property="aname" column="aname"></result>
        </collection>
    </resultMap>
    <resultMap id="userMap" type="com.itheima.domain.User">
        <id property="uid" column="uid"></id>
        <result property="username" column="username"></result>
        <result property="address" column="address"></result>
        <result property="sex" column="sex"></result>
        <result property="birthday" column="birthday"></result>
        <collection property="roles" ofType="com.itheima.domain.Role">
            <id property="rid" column="rid"></id>
            <result property="roleName" column="role_name"></result>
            <result property="roleDesc" column="role_desc"></result>
        </collection>
    </resultMap>

    <!-- p配置查询所有-->
    <select id="findAll" resultMap="userAccountMap">
        select * from user u left outer join account a on a.uid=u.uid;
    </select>
    <!-- id不能随便写要写改Dao的方法名  resultType将结果集封装到user对象中-->
   <!-- <select id="findAll" resultType="com.itheima.domain.User">
            select * from user
        </select>
    -->
    <select id="findAllUserToRole" resultMap="userMap">
        select u.*,r.rid,r.role_name,r.role_desc from user u
         left outer join user_role ur on u.uid=ur.uid
          left outer join role r on r.rid=ur.rid;
    </select>
</mapper>

log4j.propertie

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{
     ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{
     ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n


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">
<!-- mybatis的主配置文件 -->
<configuration>

    <!-- 配置环境 -->
    <environments default="mysql">
        <!-- 配置mysql的环境 -->
        <environment id="mysql">
            <!-- 配置事务类型 -->
            <transactionManager type="JDBC"></transactionManager>
            <!-- 配置数据源(连接池) -->
            <dataSource type="POOLED">
                <!-- 配置连接数据库的四个基本信息 -->
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/springs?serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="对应的密码"/>
            </dataSource>
        </environment>
    </environments>
    <!-- 指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件 -->
    <mappers>
        <mapper resource="com/itheima/dao/IAccountDao.xml"/>
        <mapper resource="com/itheima/dao/IRoleDao.xml"/>
        <mapper resource="com/itheima/dao/IUserDao.xml"/>
    </mappers>
</configuration>

你可能感兴趣的:(笔记,mybatis,xml)