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>
<!--核心配置信息-->
<environments default="mybatis_config">
<!--数据库相关配置-->
<environment id="mybatis_config">
<!--事物控制类型-->
<transactionManager type="jdbc"></transactionManager>
<!--数据库连接参数-->
<dataSource type="org.apache.ibatis.datasource.pooled.PooledDataSourceFactory">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!--& 转译成&-->
<property name="url" value="jdbc:mysql://localhost:3306/x?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
</configuration>
利用外部的jdbc.properties配置数据库相关信息(推荐)
- jdbc.properties
jdbc.username=root
jdbc.password=123456
jdbc.url=jdbc:mysql://localhost:3306/lang?useUnicode=true&characterEncoding=utf-8
jdbc.driver=com.mysql.jdbc.Driver
- maybatis-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="jdbc.properties">
</properties>
<!--别名-->
<typeAliases>
<!--二选一-->
<!-- <typeAlias type="com.lang.entity.User" alias="user"></typeAlias>-->
<package name="com.lang.entity"/>
</typeAliases>
<!--核心配置信息-->
<environments default="mybatis_config">
<!--数据库相关配置-->
<environment id="mybatis_config">
<!--事物控制类型-->
<transactionManager type="jdbc"></transactionManager>
<!--数据库连接参数-->
<dataSource type="org.apache.ibatis.datasource.pooled.PooledDataSourceFactory">
<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>
<mappers>
<mapper resource="com/lang/dao/UserDaoMapper.xml"/>
</mappers>
</configuration>
对应Dao层映射文件的配置信息
对应Dao层映射文件的头
<?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 namespace="com.lang.dao.UserDao">
<!--描述方法-->
<select id="queryUserById" resultType="User">
select * from user where id=#{arg0}
</select>
</mapper>
mybatis-config.xml写的mapper
<mappers>
<mapper resource="UserDaoMapper.xml"/>
</mappers>
测试写法
package com.lang.test;
import com.lang.dao.UserDao;
import com.lang.entity.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 javax.jws.soap.SOAPBinding;
import java.io.IOException;
import java.io.InputStream;
public class TestMybatis {
public static void main(String[] args) throws IOException {
/*加载配置信息*/
InputStream inputStream=Resources.getResourceAsStream("mybatis-config.xml");
/*构建SqlSessionFactory*/
SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream);
/*通过SqlSessionFactory创建SqlSession*/
SqlSession sqlSession=sqlSessionFactory.openSession();
/*通过SqlSession获得Dao实现类的对象*/
UserDao userDao= sqlSession.getMapper(UserDao.class);/*获取UserDao实现类的对象*/
/*测试方法*/
User user=userDao.queryUserById(1);
System.out.println(user);
}
}
mapper文件无法放进src/main/java的解决方法
在pom.xml文件写如下配置信息
<build>
<!--更改maven编译规则-->
<resources>
<resource>
<!--资源目录-->
<directory>src/main/java</directory>
<includes>
<include>*.xml</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
Druid
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
PageHelper分页依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
带了Druid和pagehelper配置信息的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="jdbc.properties">
</properties>
<!--mybaits-config.xml中开启全局缓存(默认开启)-->
<settings>
<setting name="cacheEnabled" value="true"/>
</settings>
<!--别名-->
<typeAliases>
<!-- <typeAlias type="com.lang.entity.User" alias="user"></typeAlias>-->
<package name="com.lang.entity"/>
</typeAliases>
<!--pageHelper引入-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceprtor"></plugin>
</plugins>
<!--核心配置信息-->
<environments default="mybatis_config">
<!--数据库相关配置-->
<environment id="mybatis_config">
<!--事物控制类型-->
<transactionManager type="jdbc"></transactionManager>
<!--数据库连接参数-->
<dataSource type="com.lang.datasource.MyDruidDataSourceFactory">
<property name="driverClass" value="${jdbc.driver}"/>
<!--& 转译成&-->
<property name="jdbcUrl" value="${jdbc.url}"/><!--通过${}来取配置文件的值-->
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/lang/dao/UserDaoMapper.xml"/>
</mappers>
</configuration>