1. 新建一个maven 普通java工程
image.png2. 导入依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--Mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
3. 日志配置(log4j.properties)
### 设置###
log4j.rootLogger = debug,stdout,D,E
### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%t] - [%p] %m%n
### 输出DEBUG 级别以上的日志到=E://logs/error.log ###
#log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
#log4j.appender.D.File = E://logs/log.log
#log4j.appender.D.Append = true
#log4j.appender.D.Threshold = DEBUG
#log4j.appender.D.layout = org.apache.log4j.PatternLayout
#log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 输出ERROR 级别以上的日志到=E://logs/error.log ###
#log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
#log4j.appender.E.File =E://logs/error.log
#log4j.appender.E.Append = true
#log4j.appender.E.Threshold = ERROR
#log4j.appender.E.layout = org.apache.log4j.PatternLayout
#log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
4. 创建对应数据库表的实体类和接口
目录结构User.java
package com.test.pojo;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
public class User implements Serializable {
private Integer id;
private String userName;
private Date birthday;
private String sex;
private String address;
public Integer getId() {
return id;
}
//More_get_And_Set ...
}
UserDao.java
package com.test.dao;
import com.test.pojo.User;
import java.util.List;
public interface UserDao {
List<User> findAll();
int save(User u);
}
UserDao.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.test.dao.UserDao">
<select id="findAll" resultType="com.test.pojo.User">
select * from user
</select>
<insert id="save" parameterType="com.test.pojo.User">
<!--配置添加数据后,获取该条数据的自增id,并封装到对象中-->
<selectKey keyProperty="id" keyColumn="id" resultType="int" order="AFTER">
select last_insert_id();
</selectKey>
insert into user(userName,address,sex,birthday)values (#{userName},#{address},#{sex},#{birthday})
</insert>
</mapper>
5. Mybatis配置文件(SqlMapConfig.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">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///mybatis_test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/test/dao/UserDao.xml"/>
<!--<mapper class="com.test.dao.UserDao"/>-->
<!--<package name="mapper"/>-->
</mappers>
</configuration>
6.编写测试类测试
测试目录结构
- 读取配置文件
- 创建SqlSessionFactory工厂
- 使用工厂生产SqlSession对象
- 使用SqlSession创建Dao接口的代理对象
- 使用代理对象执行方法
- 释放资源
MybatisTest.java
package com.test;
import com.test.dao.UserDao;
import com.test.pojo.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.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
public class MybatisTest {
private InputStream in;
private SqlSession sqlSession;
private UserDao userDao;
@Before
public void init() throws IOException {
//1. 读取配置文件
in = Resources.getResourceAsStream("SqlMapConfig.xml");
//2. 创建SqlSessionFactory工厂
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
//3. 使用工厂生产SqlSession对象
sqlSession = factory.openSession();
//4. 使用SqlSession创建Dao接口的代理对象
userDao = sqlSession.getMapper(UserDao.class);
}
@After
public void destroy() throws IOException {
//提交事务
sqlSession.commit();
//6. 释放资源
sqlSession.close();
in.close();
}
@Test
public void testSelect() throws IOException {
//5. 使用代理对象执行方法
List<User> userList = userDao.findAll();
for (User u : userList
) {
System.out.println(u);
}
}
@Test
public void testSave() throws IOException {
User u = new User();
u.setAddress("BJ");
u.setBirthday(new Date());
u.setSex("男");
u.setUserName("HH");
//5. 使用代理对象执行方法
userDao.save(u);
}
}
测试结果
控制台
数据库
网友评论