上一节中对 Mybatis 的基本操作有了初步的了解,
这一节中将使用 Mybatis 对数据表进行简单的 CRUD 操作.
使用的测试环境和上一篇博客一样.
使用 Mybatis 对表执行 CRUD 操作 – 基于 XML 实现
1.定义 UserMapper.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.code.mapper; import com.code.bean.UserBean; import org.apache.ibatis.annotations.Param; import java.util.List;
public interface UserMapper { UserBean getUserById(@Param("userID")int userID); int addUser(@Param("userBean")UserBean userBean); int deleteUser(@Param("userID")int userID); int updateUser(@Param("userBean")UserBean userBean); List<UserBean> getAllUsers(); List<UserBean> getAllUsersLikeName(@Param("name")String name); }
|
2.定义 sql 映射文件
userMapper.xml 文件内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| <?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.code.mapper.UserMapper">
<resultMap id="userBean" type="UserBean"> <id property="id" column="user_id" javaType="int"/> <result property="name" column="f_name" javaType="string"/> <result property="age" column="f_age" javaType="int"/> </resultMap>
<select id="getUserById" resultMap="userBean" parameterType="int"> select * from t_user where user_id = #{userID} </select>
<insert id="addUser" parameterType="UserBean" keyProperty="userBean.id" useGeneratedKeys="true"> insert into t_user values(null,#{userBean.name},#{userBean.age}) </insert> <delete id="deleteUser"> delete from t_user where user_id = #{userID} </delete> <update id="updateUser"> update t_user set f_name = #{userBean.name},f_age = #{userBean.age} where user_id=#{userBean.id} </update> <select id="getAllUsers" resultMap="userBean"> select * from t_user </select> <select id="getAllUsersLikeName" resultMap="userBean"> select * from t_user where f_name like '%${name}%' </select> </mapper>
|
3.测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| package com.code.test;
import com.code.bean.UserBean; import com.code.mapper.UserMapper; import com.code.util.DBUtil; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List;
public class Test2 { @Test public void addUserTest() { SqlSession sqlSession = DBUtil.getSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); UserBean newUser = new UserBean("张三", 26); System.out.println(userMapper.addUser(newUser)); sqlSession.commit(); System.out.println(newUser.getId()); sqlSession.close(); }
@Test public void delUserTest() { SqlSession sqlSession = DBUtil.getSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); System.out.println(userMapper.deleteUser(2)); sqlSession.commit(); sqlSession.close(); }
@Test public void updateUserTest() { SqlSession sqlSession = DBUtil.getSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); UserBean updateUser = new UserBean(1, "李四", 25); System.out.println(userMapper.updateUser(updateUser)); ; sqlSession.commit(); sqlSession.close(); }
@Test public void getAllUsersTest() { SqlSession sqlSession = DBUtil.getSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<UserBean> allUsers = userMapper.getAllUsers(); System.out.println(allUsers); }
@Test public void getAllUsersLikeNameTest() { SqlSession sqlSession = DBUtil.getSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<UserBean> allUsers = userMapper.getAllUsersLikeName("c"); System.out.println(allUsers); } }
|
使用 Mybatis 对表执行 CRUD 操作 – 基于 Annotation 实现
1.UserMapper.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| package com.code.mapper;
import com.code.bean.UserBean; import org.apache.ibatis.annotations.*;
import java.util.List;
public interface UserMapper { @Select("select * from t_user where user_id = #{userID}") UserBean getUserById(@Param("userID") int userID); @Insert("insert into t_user values(null,#{userBean.name},#{userBean.age})") int addUser(@Param("userBean") UserBean userBean); @Delete(" delete from t_user where user_id = #{userID}") int deleteUser(@Param("userID") int userID); @Update(" update t_user set f_name = #{userBean.name},f_age = #{userBean.age} where user_id=#{userBean.id}") int updateUser(@Param("userBean") UserBean userBean); @Select("select * from t_user") List<UserBean> getAllUsers(); @Select("select * from t_user where f_name like '%${name}%'") List<UserBean> getAllUsersLikeName(@Param("name") String name); }
|
将数据库连接配置信息写入到 properties 文件中
1.db.properties
1 2 3 4
| driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis name=code password=8998
|
2.在 mybatis 中引入 db.properties 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?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="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${name}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> </configuration>
|
为实体类定义别名,简化 sql 映射文件的引用
之前已经配置过
1.在 mybatis-config.xml 文件中配置如下
1 2 3
| <typeAliases> <typeAlias type="com.code.bean.UserBean" alias="UserBean"/> </typeAliases>
|
2.第二种方式:
1 2 3 4 5 6
| <typeAliases>
<package name="com.code.bean"/> </typeAliases>
|
解决字段名和实体类属性名不相同造成查询不到结果
解决办法一:
通过在查询的 sql 语句中定义字段名的别名,让字段名的别名和实体类的属性名一致,这样就可以表的字段名和实体类的属性名一一对应上了,这种方式是通过在 sql 语句中定义别名来解决字段名和属性名的映射关系的。
1
| select user_id id,f_name name, f_age age from t_user where user_id=#{userID}
|
解决办法二:
通过来映射字段名和实体类属性名的一一对应关系。这种方式是使用 MyBatis 提供的解决方式来解决字段名和属性名的映射关系的。
1 2 3 4 5
| <resultMap id="userBean" type="UserBean"> <id property="id" column="user_id" javaType="int"/> <result property="name" column="f_name" javaType="string"/> <result property="age" column="f_age" javaType="int"/> </resultMap>
|