目的: 使用Mybatis完成增删改查功能。
第一步,配置好mybatis的环境,参考博客:
https://blog.csdn.net/tomwildboar/article/details/81329060
第二步:pojo,和sqlMapConfig.xml主配置文件和上面博客中一样,就不写了。直接给出test和User.xml文件
User.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">
<!-- 写sql语句的
namespace 命名空间,这个mapper的名字
-->
<mapper namespace="test">
<!--
通过id查询一个用户
id:是待会我们调用那条sql的标识
parameterType:传入的参数的类型 (支持基本数据类型简写)
resultType:返回的数据类型
#{xxx} : 相当于占位符 xxx随便写
-->
<select id="findUserById" parameterType="Integer" resultType="com.ziyang.mybatis.pojo.User">
select * from user where id = #{a}
</select>
<!--
根据用户名模糊查询
#{} : 相对于占位符 id = #{v} == id = '1'
${} : 相当于字符串拼接 id = ${value} == id = 1
#{} 里面可以随意写 ${} 里面只能是value
-->
<select id="findUserByUsername" parameterType="String" resultType="com.ziyang.mybatis.pojo.User">
<!-- select * from user where username like '%${value}%'-->
select * from user where username like "%"#{value}"%"
</select>
<!-- 添加用户 -->
<insert id="insertUser" parameterType="com.ziyang.mybatis.pojo.User">
<!-- 获取这个条数据的id -->
<selectKey keyProperty="id" resultType="Integer" order="AFTER">
select LAST_INSERT_ID()
</selectKey>
insert into user (username,birthday,sex,address)
values (#{username},#{birthday},#{sex},#{address})
</insert>
<!-- 更新用户 -->
<update id="updateUserById" parameterType="com.ziyang.mybatis.pojo.User">
update user
set username = #{username},sex = #{sex},
birthday = #{birthday},address = #{address}
where id = #{id}
</update>
<!-- 删除 -->
<delete id="deleteById" parameterType="Integer">
delete from user
where id = #{v}
</delete>
</mapper>
test测试类
package com.ziyang.mybatis.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
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.Test;
import com.ziyang.mybatis.pojo.User;
public class Demo_1 {
/*根据id查询*/
// @Test
// public void test1() throws IOException {
// //加载核心配置文件
// InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
// //创建一个sqlSessionFactory
// SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
// //创建sqlsession
// SqlSession sqlSession = sqlSessionFactory.openSession();
// //执行sql语句
// User user = sqlSession.selectOne("test.findUserById",1);
//
// System.out.println(user);
// }
/*根据name模糊查询*/
@Test
public void test2() throws IOException {
//加载核心配置文件
InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
//创建一个sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
//创建sqlsession
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行sql语句
List<User> user = sqlSession.selectList("test.findUserByUsername","五");
for (User user2 : user) {
System.out.println(user2);
}
}
/*添加用户*/
@Test
public void test3() throws IOException {
//加载核心配置文件
InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
//创建一个sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
//创建sqlsession
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行sql语句
User user = new User();
user.setUsername("aa");
user.setBirthday(new Date());
user.setAddress("aaa");
user.setSex("a");
int i = sqlSession.insert("test.insertUser",user);
sqlSession.commit();
System.out.println(user.getId());
}
/*添加用户*/
@Test
public void test4() throws IOException {
//加载核心配置文件
InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
//创建一个sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
//创建sqlsession
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行sql语句
User user = new User();
user.setId(27);
user.setUsername("bb");
user.setBirthday(new Date());
user.setAddress("bb");
user.setSex("b");
int i = sqlSession.update("test.updateUserById", user);
sqlSession.commit();
}
//删除
@Test
public void test5() throws IOException {
//加载核心配置文件
InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
//创建一个sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
//创建sqlsession
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行sql语句
int i = sqlSession.delete("test.deleteById", 27);
sqlSession.commit();
}
}