Loading

mybatis utils核心配置文件

1.编写一个mybatis.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">
<!--configguration核心配置文件-->
<configuration>
<!--导入外部配置文件-->
<properties resource="db.properties">
<property name="username" value="root"/>
<property name="password" value="123456"/>
</properties>

<!--给实体类起别名-->
<typeAliases>
<package name="com.rzk.pojo" />
</typeAliases>

<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!--每一个Mapper.xml 都需要在Mybatis核心配置文件中注册-->
<mappers>
<mapper resource="com/rzk/dao/UserMapper.xml"/>
</mappers>

</configuration>
-----------------------------------------
2.db.properties文件
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/数据库名?userSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF8
username=root
password=123456
------------------------------------------

3.写一个工具类
public
class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static{ try { String resources="mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resources); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSessionFactory() { return sqlSessionFactory.openSession(); } }

 

posted @ 2020-03-18 18:10  Rzk  阅读(307)  评论(0编辑  收藏  举报