云社区 博客 博客详情

Mybatis学习(二)之配置文件

CSDN 发表于 2021-01-25 23:14:41 2021-01-25
0
0

【摘要】 Mybatis学习(二)之配置文件 IAccountDao package com.itheima.dao; import com.itheima.domain.Account; import com.itheima.domain.AccountUser; import java.util.List; /** * @author :班奈 * @versio...

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();
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

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; * */
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

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();
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

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>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

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>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

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



  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

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>


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

文章来源: blog.csdn.net,作者:班乃,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_43850343/article/details/112982130

登录后可下载附件,请登录或者注册

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:huaweicloud.bbs@huawei.com进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
评论文章 //点赞 收藏 0
点赞
分享文章到微博
分享文章到朋友圈

上一篇:电商数据库核心表设计

下一篇:Oracle基本操作+Java连接Oracle

评论 (0)


登录后可评论,请 登录注册

评论