(四)Mybatis总结之接口映射
前面Mybatis是直接通过Dao层与数据交互,更好的方法是Mybatis通过接口映射方式与数据交互
1.在项目中添加maven支持(即pom.xml下添加支持)
<!--
在pom.xml下配置
-->
<!-- mybatis核心包 -->
<dependencies>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<!-- mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<!-- junit测试包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 日志文件管理包 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
</dependencies>
Maven要按照大佬博客说的配置,接下来file-->settings-->Maven-->Maven home directory设置为
Maven安装包路径
然后user settings file 设置为安装包路径
\conf\settings.xml
2.创建实体类UserInfo
package com.qf.mybatisdemo.pojo;
import java.util.List;
public class UserInfo {
int userId;
String password;
String email;
String phone;
String icon;
String userName;
String name;
List<RoleInfo> roleInfoList;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<RoleInfo> getRoleInfoList() {
return roleInfoList;
}
public void setRoleInfoList(List<RoleInfo> roleInfoList) {
this.roleInfoList = roleInfoList;
}
@Override
public String toString() {
return "UserInfo{" +
"userId=" + userId +
", password='" + password + '\'' +
", email='" + email + '\'' +
", phone='" + phone + '\'' +
", icon='" + icon + '\'' +
", userName='" + userName + '\'' +
", name='" + name + '\'' +
", roleInfoList=" + roleInfoList +
'}';
}
}
3.创建mapper包,mapper下创建UserMapper接口
package com.qf.mybatisdemo.mapper;
import com.qf.mybatisdemo.pojo.UserInfo;
import com.qf.mybatisdemo.userVo.UserVo;
import java.util.List;
public interface UserInfoMapper {
<!--查询-->
public UserVo userInfoLogin(UserVo userVo);
<!--public int addUserInfo(UserVo userVo);-->
<!--public int updateUserInfobyId(UserVo userVo);-->
<!--public int deleteUserInfo(UserVo userVo);-->
<!--public List<UserInfo> getUserInfo(UserInfo userInfo);-->
}
4.resource资源包下创建mapper包,mapper下创建UserMapper.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.qf.mybatisdemo.mapper.UserInfoMapper"><!--注意此处-->
<select id="UserInfoLogin" parameterType="userVo" resultType="userVo">
select * from userinfo
<where>
<if test="userName != null and userName == ''">
and username=#{userName}
</if>
<if test="password != null and password == ''">
and password =#{password}
</if>
</where>
</select>
</mapper>
namespace
一般绑定对应的文件的全路径,
- 有三种全路径:
-
第一种:namespace绑定实体类的全路径:当namespace绑定的是实体类的全路径时,其实现数据持久化的方式为无代理模式实现数据持久化。需要手动实现dao层的接口。
<mapper namespace="com.qf.pojo.User"></namespace>
-
第二种:namespace绑定dao层接口的全路径:当namespace绑定的是dao接口的全路径时,其实现数据持久化的方式为有代理模式实现数据持久化。即会自动产生代理,自动实现数据的持久化,不需要实现dao层的接口。
<mapper namespace="com.qf.dao.userDao"></namespace>
-
第三种:namespace绑定的是mapper接口对应的sql.xml文件是,其也是有代理模式自动实现数据持久化。但mapper接口对应的sql.xml文件名必须保持一致才能自动实现数据持久化。
<mapper namespace="com.qf.mapper.UserInfoMapper"></namespace>
-
5.resource下创建log4j.properties,配置log4j日志
log4j.rootLogger=DEBUG,CONSOLE,file
#log4j.rootLogger=ERROR,ROLLING_FILE
#下面log4j.logger.com.qf.dao=debug 根据项目目录来改
log4j.logger.com.qf.dao=debug
log4j.logger.com.ibatis=debug
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
#log4j.logger.java.sql.Connection=debug
#log4j.logger.java.sql.Statement=debug
#log4j.logger.java.sql.PreparedStatement=debug
#log4j.logger.java.sql.ResultSet=debug
log4j.logger.org.tuckey.web.filters.urlrewrite.UrlRewriteFilter=debug
######################################################################################
# Console Appender \u65e5\u5fd7\u5728\u63a7\u5236\u8f93\u51fa\u914d\u7f6e
######################################################################################
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=error
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= [%p] %d %c - %m%n
######################################################################################
# DailyRolling File \u6bcf\u5929\u4ea7\u751f\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u683c\u5f0f:log2009-09-11
######################################################################################
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern=yyyy-MM-dd
log4j.appender.file.File=log.log
log4j.appender.file.Append=true
log4j.appender.file.Threshold=error
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L)%m%n
log4j.logger.com.opensymphony.xwork2=error
6.resource下创建mybatis-config.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="mysql.properties"/>
<settings>
<!--全局性设置懒加载。如果设为‘false’,则所有相关联的都会被初始化加载,默认值为false-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--当设置为‘true’的时候,懒加载的对象可能被任何懒属性全部加载。否则,每个属性都按需加载。默认值为true-->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<typeAliases>
<!-- 将com.qf.mybatisdemo.pojo.User的替换成一个短的名字user-->
<typeAlias type="com.qf.mybatisdemo.pojo.User" alias="user"/>
</typeAliases>
<!--对事务的管理和连接池的配置-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED"><!--POOLED:使用Mybatis自带的数据库连接池来管理数据库连接-->
<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>
<!--resource下mapper的xml文件路径配置-->
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
<mapper resource="mapper/UserInfoMapper.xml"/>
<!--<mapper resource="mapper/RoleInfoMapper.xml"/>-->
<!--<mapper resource="mapper/MenuInfoMapper.xml"/>-->
</mappers>
</configuration>
7.resource下创建mysql.propertis
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/1901mybatis?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
jdbc.username=root
jdbc.password=root
8.创建测试类
package com.qf.mybatisdemo;
import com.qf.mybatisdemo.dao.BaseDao;
import com.qf.mybatisdemo.mapper.UserInfoMapper;
import com.qf.mybatisdemo.pojo.UserInfo;
import com.qf.mybatisdemo.userVo.UserVo;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class MybatisDemo2 {
BaseDao baseDao = new BaseDao();<!--调用前面的BaseDao曾-->
SqlSession sqlSession = baseDao.getSqlSession();
UserInfoMapper userInfoMapper = sqlSession.getMapper(UserInfoMapper.class);
@Test
public void UserInfoLogin() {
UserVo userVo =new UserVo();
UserVo userVo2 =new UserVo();
userVo.setPassword("123456");
userVo.setUserName("liaomou");
userVo2 = userInfoMapper.userInfoLogin(userVo);
System.out.println(userVo2);
}
}