十年河东,十年河西,莫欺少年穷
学无止境,精益求精
MyBatis标签如下:
Mybatis 标签在配置文件中要按照上图顺序进行配置。
如果你在Idea中将顺序配错了,Idea会有一个提示,如下:
1、properties
properties 用于引入外部 .properties文件,最常用的是引入数据库配置文件,常用语法为:
"db.properties"/>
在mybatis-config.xml同录下下创建db.properties,如下
Jbdc.driver = com.mysql.cj.jdbc.Driver
Jbdc.url = jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
Jbdc.username = root
Jbdc.password = chen1234567890
2、settings
settings 用于配置MyBatis缓存、懒加载等,常用配置如下:
<settings>
<!--开启全局缓存 默认true-->
<setting name="cacheEnabled" value="true"/>
<!--开启全局懒加载 默认false-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--开启关联属性懒加载 默认false-->
<setting name="aggressiveLazyLoading" value="true"/>
<!--全局开启驼峰映射-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
3、typeAliases
typeAliases 用于为映射文件中的 resultType 设置别名。
例如下面这个映射文件
<?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">
<!--namespace=绑定一个指定的Dao/Mapper接口-->
<mapper namespace="com.one5a.model.User">
<select id="getUserList" resultType="com.one5a.model.User">
select * from users
</select>
<select id="getUserbyId" resultType="com.one5a.model.User" parameterType="int">
select * from users where uid=#{id}
</select>
</mapper>
View Code
resultType="com.one5a.model.User" 比较长,用起来不方便,可以为它设置一个别名,这样写起来方便快捷
<typeAliases>
<typeAlias alias="User" type="com.one5a.model.User"></typeAlias>
</typeAliases>
上述这种写法简洁了配置,但是如果一个包下面有100个实体,我们就需要配置100次,因此引入包扫描配置法
包扫描配置
<typeAliases>
<!-- <typeAlias alias="User" type="com.one5a.model.User"></typeAlias> -->
<package name="com.one5a.model"/>
</typeAliases>
com.one5a.model 包下的实体默认均使用实体名称【不区分大小写】。
例如,count(*) 返回值为int ,因此resultType 可以使用默认别名 int
<select id="getTotal" resultType="int" parameterType="int">
select count(*) from users where uid=#{id}
</select>
@Test
public void getTotal() throws IOException {
String resource = "mybatis-config.xml";
//使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
//InputStream is = Resources.getResourceAsStream(resource);
//InputStream is = Tests.class.getClassLoader().getResourceAsStream(resource);
InputStream is =Resources.getResourceAsStream(resource);
//构建sqlSession的工厂 线程安全 建议使用单列模式
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
//绝对不能将它作为静态对象使用,不可共享
SqlSession session = sessionFactory.openSession();
/**
* 映射sql的标识字符串,
* com.oumyye.mapping.UserMapping是userMapper.xml文件中mapper标签的namespace属性的值,
* getUser是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL
*/
String statement = "com.one5a.model.User.getTotal";//映射sql的标识字符串
//执行查询返回一个唯一user对象的sql
Integer count = session.selectOne(statement,1);
System.out.println(count);
}
View Code
environments
environments用于配置运行环境
在数据库配置信息中增加生产环境用户名和密码
Jbdc.driver = com.mysql.cj.jdbc.Driver
Jbdc.url = jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
Jbdc.username = root
Jbdc.password = chen1234
prd.username = root
prd.password = chen1234
View Code
<environments default="prd">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${Jbdc.driver}"/>
<property name="url" value="${Jbdc.url}"/>
<property name="username" value="${Jbdc.username}"/>
<property name="password" value="${Jbdc.password}"/>
</dataSource>
</environment>
<environment id="prd">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${Jbdc.driver}"/>
<property name="url" value="${Jbdc.url}"/>
<property name="username" value="${prd.username}"/>
<property name="password" value="${prd.password}"/>
</dataSource>
</environment>
</environments>
default = “prd” 使用的是生产环境,dataSource type=“POOLED”,使用默认数据库连接池的模式
Mappers
<!-- 注册UserMapper.xml -->
<mappers>
<mapper resource="Mapper/UserMapper.xml"/>
</mappers>
最后贴出Mybatis核心配置文件 和 用于映射文件
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="db.properties"/>
<settings>
<!--开启全局缓存 默认true-->
<setting name="cacheEnabled" value="true"/>
<!--开启全局懒加载 默认false-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--开启关联属性懒加载 默认false-->
<setting name="aggressiveLazyLoading" value="true"/>
<!--全局开启驼峰映射-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<!--别名方式-->
<!-- <typeAlias alias="User" type="com.one5a.model.User"></typeAlias> -->
<!--包扫描方式-->
<package name="com.one5a.model"/>
</typeAliases>
<environments default="prd">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${Jbdc.driver}"/>
<property name="url" value="${Jbdc.url}"/>
<property name="username" value="${Jbdc.username}"/>
<property name="password" value="${Jbdc.password}"/>
</dataSource>
</environment>
<environment id="prd">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${Jbdc.driver}"/>
<property name="url" value="${Jbdc.url}"/>
<property name="username" value="${prd.username}"/>
<property name="password" value="${prd.password}"/>
</dataSource>
</environment>
</environments>
<!-- 注册UserMapper.xml -->
<mappers>
<mapper resource="Mapper/UserMapper.xml"/>
</mappers>
</configuration>
View Code
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">
<!--namespace=-->
<mapper namespace="com.one5a.model.User">
<select id="getUserList" resultType="User">
select * from users
</select>
<select id="getUserbyId" resultType="User" parameterType="int">
select * from users where uid=#{id}
</select>
<select id="getTotal" resultType="int" parameterType="int">
select count(*) from users where uid=#{id}
</select>
</mapper>
View Code
UserMapper 属于映射文件,普通模式下,UserMapper文件的命名空间值和【实体所在的包+实体名称】保持一致,接口编程情况下,和 【接口所在的包+接口名称】保持一致。
Mybatis接口式编程
新建接口 com.one5a.Inter.IUser
接口必须实现UserMapper中定义的所有方法,如下
package com.one5a.Inter;
import com.one5a.model.User;
import java.util.List;
public interface IUser {
List<User> getUserList();
User getUserbyId(Integer id);
Integer getTotal(Integer id);
}
修改UserMapper的命名空间为【接口所在的包+接口名称】,即:com.one5a.Inter.IUser
光标放在接口上,Alt+Insert 快捷键,生成测试类
package com.one5a.Inter;
import com.one5a.model.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.InputStream;
import java.util.List;
import static org.junit.Assert.*;
public class UserTest {
IUser user=null;
@Before
public void setUp() throws Exception {
String resource = "mybatis-config.xml";
//使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
//InputStream is = Resources.getResourceAsStream(resource);
//InputStream is = Tests.class.getClassLoader().getResourceAsStream(resource);
InputStream is = Resources.getResourceAsStream(resource);
//构建sqlSession的工厂 线程安全 建议使用单列模式
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
//绝对不能将它作为静态对象使用,不可共享
SqlSession session = sessionFactory.openSession();
//创建接口动态代理对象
user=session.getMapper(IUser.class);
}
@Test
public void getUserList() {
List<User> userLst=user.getUserList();
System.out.println(userLst);
}
@Test
public void getUserbyId() {
User userLst=user.getUserbyId(1);
System.out.println(userLst);
}
@Test
public void getTotal() {
Integer cnt =user.getTotal(2);
System.out.println(cnt);
}
}
View Code
注意代码中的动态生成代理对象,这句话为接口编程的核心。
@天才卧龙