2.简单的mybatis项目
2.1 建库,建表
create database mybatis default charset utf8;
use mybatis;
create table t_user(
id int primary key auto_increment,
name varchar(40),
pwd varchar(20),
age int
)engine=innodb default charset=utf8;
2.2 添加jar包
-
mybatis-版本号.jar
-
数据库驱动jar包
-
其他依赖的jar包
jsqlparser-0.9.5.jar log4j-1.2.17.jar pagehelper-4.1.6.jar
2.3 配置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>
<!-- mybatis的多个配置环境 -->
<!-- default的值是 具体的 环境的id, 表示当前使用该环境的配置 -->
<environments default="abc">
<environment id="abc">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8"></property>
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</dataSource>
</environment>
<environment id="qwe">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="url" value="jdbc:mysql://localhost:3306/shopping?useUnicode=true&characterEncoding=utf8"></property>
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</dataSource>
</environment>
</environments>
<!-- 配置多个映射 -->
<mappers>
<!-- resource的值是 mapper配置的相对src的路径 -->
<mapper resource="com/itany/ms/mapper/UserMapper.xml" ></mapper>
<!-- 如果使用的是注解,则使用class属性,其值是dao接口的全名 -->
<!--<mapper class="com.itany.ms.dao.UserDao" ></mapper>-->
</mappers>
</configuration>
2.4 配置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">
<mapper namespace="userMapper">
<insert id="insertUser">
insert into t_user
(name,pwd,age)
values
('abc', '123', 22)
</insert>
</mapper>
2.5 编写测试代码
package com.itany.ms;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class Test01 {
SqlSession sqlSession;
@Before
public void before(){
// 1.创建SqlSessionFactory构建器, 用来加载mybatis-config.xml配置文件
SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
// 2.加载mybatis-config.xml配置文件, 创建SqlSession工厂,用来获取sqlSession
SqlSessionFactory factory = ssfb.build(Test01.class
.getClassLoader()
.getResourceAsStream("mybatis-config.xml"));
// 3.获取sqlSession
// 默认会开启事务,不自动提交事务,且不手动提交时,默认结束时执行回滚。
sqlSession = factory.openSession();
System.out.println("init");
}
@After
public void after(){
// 关闭
sqlSession.close();
System.out.println("end");
}
@Test
public void test1(){
// 字符串是 mapper文件的 namespace + "." + insert标签的id值
sqlSession.insert("userMapper.insertUser");
// 提交事务
sqlSession.commit();
}
}
2.6 打开调试日志
# 日志的简单配置
log4j.appender.myconsole=org.apache.log4j.ConsoleAppender
log4j.appender.myconsole.layout=org.apache.log4j.PatternLayout
log4j.appender.myconsole.layout.ConversionPattern=%5p %c %m%n
# 控制mybatis的结果集等日志输出
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.PreparedStatemant=DEBUG
# 输出的日志等级 需要调为 debug
log4j.rootLogger=debug,myconsole