1.创建数据库
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) NOT NULL COMMENT '用户名称',
`birthday` date DEFAULT NULL COMMENT '生日',
`sex` char(1) DEFAULT NULL COMMENT '性别',
`address` varchar(256) DEFAULT NULL COMMENT '地址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8;
创建数据库.png
2.创建普通java工程
创建普通java工程.png3.导包
导包.png4.建工程结构及 jdbc 、log 配置
工程结构.png 建工程结构及 jdbc 、log 配置.png5.配置 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:核心配置文件,数据源,事务 -->
<configuration>
// properties:引入外部资源文件
<properties resource="jdbc.properties"></properties>
<typeAliases>
// 批量扫描定义别名:表示com.xiaocai.domain包路径下所有类都自动被定义了别名 别名名称:类名,大小写随意
<package name="com.xiaocai.mybatis.domain" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
// 引入外部映射文件:引入多个映射文件
<mappers>
<package name="com.xiaocai.dao"/>
</mappers>
</configuration>
6.编写 domain 类 -- User
编写 domain 类.png7.编写操作数据库接口类
public interface UserDao {
// 根据Id查询用户
public User findUserByID(Integer id);
// 根据用户名模糊查询
public List<User> findUserWithLike(String username);
// 插入数据
public Integer insertUser(User user);
}
8.建同包名,名字也相同的 dao 对应的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.xiaocai.dao.UserDao">
<!-- 根据id获取用户信息 -->
<select id="findUserByID" parameterType="int"
resultType="com.xiaocai.domain.User">
select * from user where id = #{id}
</select>
<!-- 自定义条件查询用户列表 -->
<select id="findUserWithLike" parameterType="java.lang.String"
resultType="com.xiaocai.domain.User">
select * from user where username like '%${value}%'
</select>
<!-- 添加用户 -->
<insert id="insertUser" parameterType="com.xiaocai.domain.User">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
select LAST_INSERT_ID()
</selectKey>
insert into user(username,birthday,sex,address)
values(#{username},#{birthday},#{sex},#{address})
</insert>
</mapper>
同包名文件名.png
9.编写测试类
public class TestDao {
private SqlSessionFactory sqlSessionFactory;
private InputStream resourceAsStream;
@Before
public void beforeTest() throws IOException {
String resourceFile = "sqlMapConfig.xml";
resourceAsStream = Resources.getResourceAsStream(resourceFile);
sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(resourceAsStream);
}
}
10.mybatis 测试
- 1.插入数据
@Test
public void insertUserTest() {
SqlSession openSession = sqlSessionFactory.openSession();
UserDao mapper = openSession.getMapper(UserDao.class);
User user = new User();
user.setUsername("xiaocai");
user.setSex("男");
Integer insertUser = mapper.insertUser(user);
// 提交
openSession.commit();
// 关闭session
openSession.close();
System.out.println(insertUser);
}
测试结果.png
- 2.查询
@Test
public void findByIDTest() {
SqlSession openSession = sqlSessionFactory.openSession();
UserDao userDaoMapper = openSession.getMapper(UserDao.class);
User user = userDaoMapper.findUserByID(30);
System.out.println(user);
}
网友评论