Mybatis学习总结第二篇
一、在pom.xml中导入相关依赖
- mysql-connector-java
- mybatis
- junit
二、编写MyBatis配置文件
<configuration>
<!-- 配置环境:可以配置多个环境,default:配置某一个环境的唯一标识,表示默认使用哪个环境 -->
<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://localhost:3306/mybatis?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 配置映射文件:用来配置sql语句和结果集类型等 -->
<!--每一个Mapper.xml文件都需要在Mybatis核心配置文件中注册-->
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
三、编写工具类MybatisUtils
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static{
try {
//第一步:使用Mybatis获取sqlSessionFactory对象
String resource = "mybatis-config.xml";
//创建流
InputStream inputStream = Resources.getResourceAsStream(resource);
//加载流
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//第二步:获取SqlSession实例
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
四、编写User实体类
- 有参构造、无参构造、get\set方法以及toString方法省略…
private int id;
private String username;
private String password;
private String sex;
private String address;
五、编写UserMapper接口
- 查询所有用户
- 根据id查询用户
- 添加用户
- 修改用户
- 删除用户
public interface UserMapper {
//查询所有用户
List<User> getUserList();
//根据id查询用户(返回值为User)
User getUserById(int id);
//添加用户
int addUser(User user);
//修改用户
int updateUser(User user);
//删除用户
int deleteUser(int id);
}
六、编写映射文件
- 注意:要在配置文件中加载映射文件
<mapper namespace="com.dmj.dao.UserMapper">
<!--id对应Mapper中的方法名字-->
<select id="getUserList" resultType="com.dmj.entity.User">
select * from mybatis.user1
</select>
<!--根据id查询用户-->
<select id="getUserById" resultType="com.dmj.entity.User">
select * from mybatis.user1 where id = #{id}
</select>
<!--添加用户-->
<insert id="addUser" parameterType="com.dmj.entity.User">
insert into mybatis.user1 (id, username, password, sex,address) values (#{id},#{username},#{password},#{sex},#{address})
</insert>
<!--修改用户-->
<update id="updateUser" parameterType="com.dmj.entity.User">
update mybatis.user1 set username = #{username},password = #{password},sex = #{sex},address= #{address} where id = #{id} ;
</update>
<!--删除用户-->
<delete id="deleteUser" parameterType="com.dmj.entity.User">
delete from mybatis.user1 where id = #{id}
</delete>
</mapper>
七、测试(实现增删改查操作)
- 注意:增删改需要提交事务 sqlSession.commit();
public class UserDaoTest {
@Test
//查询所有用户
public void test(){
//获得SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//方式一:getMapper(推荐使用)
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserList();
//方式二:
//List<User> userList = sqlSession.selectList("com.dmj.dao.UserMapper.getUserList");
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();//关闭
}
//通过id查询用户
@Test
public void getUserById(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUserById(2);
System.out.println(user);
sqlSession.close();
}
//添加用户
@Test
public void addUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int res = mapper.addUser(new User(3,"王五","123456","男","浙江丽水"));
if (res>0){
System.out.println("添加成功!");
}
//提交事务
sqlSession.commit();
sqlSession.close();
}
//修改用户
@Test
public void updateUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int res = mapper.updateUser(new User(3,"哈哈","654321","女","浙江金华"));
if (res>0){
System.out.println("修改成功!");
}
//提交事务
sqlSession.commit();
sqlSession.close();
}
//删除用户
@Test
public void deleteUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int res = mapper.deleteUser(3);
if (res>0){
System.out.println("删除成功!");
}
//提交事务
sqlSession.commit();
sqlSession.close();
}
}
八、数据库操作
-
查询所有用户
-
根据id查询用户
-
添加用户
-
修改用户
-
删除用户
九、总结
本次学习总结主要是为了在MyBatis中实现增删改查的操作,大部分都是差不多的,需要注意的一点就是增删改需要提交事务。