初识Mybatis
初识Mybatis
什么是Mybatis
Mybatis框架也称为ORM(Object Relational Mapper,对象关系映射) 框架。为了解决面向对象和关系型数据库中数据不匹配的技术。通过描述Java对象与数据库表之间的映射关系自动将Java应用程序的对象持久化到关系型数据库表中
过程
依赖
User.java
package com.sty.pojo1;
import lombok.Data;
@Data
public class User {
private int id;
private String name;
private String password;
}
UserDao.java
package com.sty.Mapper;
import com.sty.pojo1.User;
import java.util.List;
public interface UserDao {
User queryUserById(int id);
List<User> queryAll();
}
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.sty.Mapper.UserDao">
<select id="queryUserById" parameterType="int" resultType="user">
select *
from mybatis.user
where id=#{id}
</select>
<select id="queryAll" resultType="user">
select *
from mybatis.user;
</select>
</mapper>
mybatis-config.xml
<?xml version="1.0" encoding="UTF8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!---->
<configuration>
<!--配置数据库-->
<properties resource="db.properties"/>
<!--log4j配置-->
<settings>
<!--value是大写-->
<setting name="logImpl" value="LOG4J"/>
</settings>
<!--扫描包-->
<typeAliases>
<typeAlias type="com.sty.pojo1.User" alias="user"/>
</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>
<mappers>
<mapper resource="com/sty/Mapper/UserMapper.xml"/>
</mappers>
</configuration>
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&userSSL=true
username=root
password=root
log4j.properties
#将等级为BUG的日志文件输出到控制泰console和文件file中#
log4j.rootLogger=DEBUG,console,file
#控制台输出配置#
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%c]-%m%n
#文件输出相关设置#
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/sty.log
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}[%c]%m%n]
#日志输出级别#
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PrepareStatement=DEBUG
Test
package com.sty;
import com.sty.Mapper.UserDao;
import com.sty.pojo1.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.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MyTest {
@Test
public void QueryUserByIdTest() throws IOException {
//读取配置文件
InputStream inputStream = Resources.getResourceAsStream("Mybatis-config.xml");
//根据配置文件,实例化SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//通过SqlSessionFactory,实例化SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
User user = mapper.queryUserById(1);
System.out.println(user);
List<User> users = mapper.queryAll();
for (User user1 : users) {
System.out.println(user1);
}
//关闭
sqlSession.close();
}
}