1.需要引入的依赖:
2.创建全局配置文件mybatis-config:
2.1:创建dbconfig.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
2.2:配置mybatis环境并将sql映射文件注册到全局配置文件中
<?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 标签
1、mybatis可以使用properties标签来引入properties配置文件的内容
resource:引入类路径下的资源
url:引入网络路径或者磁盘路径下的资源
-->
<properties resource="dbconfig.properties"></properties>
<!--settings标签
1.settings包含很多重要的设置项
setting:用来设置每一个设置项
name:设置项名
value:设置项取值
-->
<settings>
<!--mapUnderscoreToCamelCase 开启驼峰命名规范,将下划线后的字母改为大写(并消除下划线)-->
<setting name="mapUnderscoreToCamelCase" value="true"></setting>
</settings>
<!--typeAliases
typeAliases:别名处理器,可以为java类型起别名:
1.typeAliases:为某个java类型起别名
type:指定要起别名的类型的全类名,默认为类名首字母小写:employee
alias;指定新的别名
2.package:为某个包下的所有类批量起别名(默认别名)
name:指定包名
3.可以在需要起别名的类上加上@Alias()为其起别名
-->
<typeAliases>
<!--<typeAlias type="mybatis.bean.Employee" alias="employee"></typeAlias>-->
<package name="mybatis.bean"></package>
</typeAliases>
<!--environments标签,mybatis可以配置多种环境
environment:配置具体环境信息,必须有两个标签;id代表这个环境的唯一标识
transactionManager:事务管理器
type:事务管理器的类型;JDBC|MANAGED
dataSource:数据源
type:数据源类型 UNPOOLED|POOLED|JNDI
-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<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:将一个sql映射注册到全局配置中
resource:应用类路径下的sql映射文件
url:引用网络或者磁盘路径下
class:引用(注册)接口
package:批量注册
-->
<!-- 将我们写好的sql映射文件(EmployeeMapper.xml)一定要注册到全局配置文件(mybatis-config.xml)中 -->
<mappers>
<mapper resource="EmployeeMapper.xml" />
</mappers>
</configuration>
3.创建SQL映射文件
3.1 接口式编程: (创建与SQL映射文件相对于的接口)
package mybatis.dao;
import mybatis.bean.Employee;
public interface EmployeeMapper {
public Employee getEmpById(Integer id);
}
namespace:指定接口的全类名,id:接口中对应的方法
<?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="mybatis.dao.EmployeeMapper">
<!--
namespace:名称空间;指定为接口的全类名
id:唯一标识,写成接口的方法
resultType:返回值类型
#{id}:从传递过来的参数中取出id值
public Employee getEmpById(Integer id);
-->
<select id="getEmpById" resultType="employee">
select id,last_name ,email,gender from tbl_employee where id = #{id}
</select>
</mapper>
4.测试:
//每次连接都需要执行:1)根据全局配置文件得到SqlSessionFactory
//将其抽取成方法
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
return sqlSessionFactory;
}
@Test
public void test2() throws IOException {
//1)根据全局配置文件得到SqlSessionFactory
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//2)使用SqlSessionFactory获取SqlSession对象,使用他来执行增删改查
SqlSession openSession = sqlSessionFactory.openSession();
//3)获取接口的实现类对象
//会为接口创建一个代理对象,代理对象执行增删改查
try {
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
Employee employee = mapper.getEmpById(1);
System.out.println(employee);
} finally {
//一个SqlSession就是代表和数据库的一次会话,用完关闭
openSession.close();
}
}