Mybatis 初体验
Mybatis 相关依赖以及配置文件的约束
相关依赖
<!--log4j 日志依赖-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<!--mysql 数据源-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.6</version>
</dependency>
xml文件的约束
config 文件的约束
<?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">
mapper文件的约束
<?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">
Mybatis First Project
1. 创建一个Maven工程
2. 在Maven项目中创建如下文件
3. 数据源的配置文件 mydbconfig.properties
注意:每行不能以空格结尾,不然会出错
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/ttt
db.username=root
db.password=admin
4. mybatis-config 总配置文件
<?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="mydbconfig.properties" />
<!--配置Mybatis 环境-->
<environments default="development">
<!--配置mysql环境-->
<environment id="development">
<!--配置事务的类型-->
<transactionManager type="JDBC"/>
<!--配置链接数据库的信息 用的是数据源(连接池)-->
<dataSource type="POOLED">
<property name="driver" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</dataSource>
</environment>
</environments>
<!--告知Mybatis Mapper映射文件的位置-->
<mappers>
<mapper resource="mapper/UserMapper.xml"></mapper>
</mappers>
</configuration>
5. log4j.properties配置文件
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
6. 创建实体类UserBean实体类并实现set get方法
注意:如果没有在对应的映射文件中配置 resultMap,此时的实体类中的属性名必须与数据库中表中字段的名字一致
7. 创建持久层接口UserMapper
8. 编写持久层接口UserMapper的映射文件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.mapper.UserMapper">
<!--id 为你接口中的方法 parameterType参数类型 resultType 返回的类型 全限定类名-->
<select id="getUserById" parameterType="int" resultType="com.beans.UserBean">
select * from user where id = #{id}
</select>
</mapper>
9. 编写测试类
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JAr7WsR7-1584109798367)(C:\Users\hwy20\AppData\Roaming\Typora\typora-user-images\image-20200311112338661.png)]
主要代码:
//加载类路径下的配置文件
//1.InputStream in= Resources.getResourceAsStream("mybatis-config.xml");
InputStream in = Test.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
//2.创建SqlSessionFactory的构建者对象
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//3.使用构建者对象创建工厂对象SqlSessionFactory
SqlSessionFactory factory = builder.build(in);
//4.使用SqlSessionFactory产生sqlSession对象
SqlSession sqlSession = factory.openSession();
//5.使用sqlSession创建接口userMapper的代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//6.使用代理对象调用查询方法
UserBean user = userMapper.getUserById(5);
System.out.println(user);
sqlSession.close();
10. 利用Junit进行测试 ----推荐
测试类中的内容
SqlSession sqlSession = null;
UserMapper userMapper = null;
// 在测试方法之前执行
@Before
public void init() {
// 1.加载类路径下的配置文件
// 2.InputStream in= Resources.getResourceAsStream("mybatis-config.xml");
InputStream in = Test.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
// 3.创建SqlSessionFactory的构建者对象
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
// 4.使用构建者对象创建工厂对象SqlSessionFactory
SqlSessionFactory factory = builder.build(in);
// 5.使用SqlSessionFactory产生sqlSession对象
sqlSession = factory.openSession();
//6.使用sqlSession创建接口userMapper的代理对象
userMapper = sqlSession.getMapper(UserMapper.class);
}
// 在测试方法之后执行
@After
public void destory() {
sqlSession.close();
}
@Test
public void testSelectLikeName() {
//7.使用代理对象调用查询方法
List<UserBean> users = userMapper.getListUserLikeName("hwy");
for (UserBean user : users) {
System.out.println(user);
}
}
本人公众号,欢迎交流