1、创建工程MyBatisDAO
2、引入依赖
mybatis的、数据库的、junit的
3、创建资源文件夹
项目上右单击---new---Sou'r'ce Folder
4、创建包、类、及配置文件
1、2、3、4、5是步骤
5、Userinfo.java类里的内容
package com.boxue.mybatis.entity;
import java.sql.Date;
public class Userinfo {
private int id;
private String username;
private String sex;
private Date birthday;
private String address;
//提供get、set、toString方法
}
6、UserinfoDao.java接口里的内容
package com.boxue.mybatis.mapper;
import com.boxue.mybatis.entity.Userinfo;
public interface UserinfoDao {
public Userinfo findUserById(int id);
public void insertUserinfo(Userinfo u);
}
7、UserinfoDaoImpl类里的内容
package com.boxue.mybatis.mapper;
import java.sql.Date;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.boxue.mybatis.entity.Userinfo;
public class UserinfoDaoImpl implements UserinfoDao{
private SqlSessionFactory sqlSessionFactory;
//构造器
public UserinfoDaoImpl(SqlSessionFactory sqlSessionFactory){
this.sqlSessionFactory=sqlSessionFactory;
}
@Override
public Userinfo findUserById(int id) {
SqlSession sqlSession = sqlSessionFactory.openSession();
Userinfo u = sqlSession.selectOne("test.findUserById",id);
sqlSession.close();
return u;
}
@Override
public void insertUserinfo(Userinfo u) {
SqlSession sqlSession = sqlSessionFactory.openSession();
int b = sqlSession.insert("test.insertUserinfo",u);
sqlSession.commit();
sqlSession.close();
}
}
8、UserinfoDatTest.java类里的内容
package com.boxue.mybatis.test;
import java.io.IOException;
import java.io.InputStream;
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.Before;
import org.junit.Test;
import com.boxue.mybatis.entity.Userinfo;
import com.boxue.mybatis.mapper.UserinfoDao;
public class UserinfoDatTest {
private SqlSessionFactory sqlSessionFactory;
@Before
public void setUp() throws IOException{
String resource="SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void TestMethod(){
//根据id查询用户信息
UserinfoDao ud = new UserinfoDaoImpl(sqlSessionFactory);
Userinfo u = ud.findUserById(1);
System.out.println(u);
}
}
9、UserinfoMapper.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="test">
<!-- 根据id查询用户信息 -->
<select id="findUserById" parameterType="int" resultType="com.boxue.mybatis.entity.Userinfo">
select * from userinfo where id=#{id}
</select>
<!-- 添加用户信息 -->
<insert id="insertUserinfo" parameterType="com.boxue.mybatis.entity.Userinfo">
insert into userinfo(username,sex,birthday,address)
values(#{username},#{sex},#{birthday},#{address})
</insert>
</mapper>
10、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.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatisdemo?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="999999"/>
</dataSource>
</environment>
</environments>
<!-- 用于配置映射文件的标记 -->
<mappers>
<mapper resource="mapper/UserinfoMapper.xml"/>
</mappers>
</configuration>
11、运行测试类,进行测试
测试成功