这次接着上次写增删改查吧。
现将上节的方法改造一下,改造测试类。
package cn.my.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 cn.my.mybatis.entity.User; public class TestUser { private SqlSessionFactory factory; @Before public void setUp() throws IOException{ String resource = "SqlMapConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); factory = new SqlSessionFactoryBuilder().build(inputStream); } @Test public void testFindById(){ SqlSession session = factory.openSession(); User user = session.selectOne("cn.my.mybatis.entity.User.findById", 1); session.close(); System.out.println(user); } }
简单的改造一下是为了后面写增删改查是每次都需要写setup方法中的代码。
下面我就不贴图了,我直接写代码了
1.新增
1.1首先添加新增的映射文件
<?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="cn.my.mybatis.entity.User"> <select id="findById" parameterType="int" resultType="cn.my.mybatis.entity.User"> select * from user where id=#{id} </select> <!-- 新增映射 --> <insert id="insertUser" parameterType="cn.my.mybatis.entity.User"> INSERT INTO `user` (`username`,`birthday`,`sex`,`address`) VALUES(‘id‘,‘username‘,‘birthday‘,‘sex‘,‘address‘) </insert> </mapper>
1.2 .添加新增测试方法
package cn.my.test; import java.io.IOException; import java.io.InputStream; import java.util.Date; 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 cn.my.mybatis.entity.User; public class TestUser { private SqlSessionFactory factory; @Before public void setUp() throws IOException{ String resource = "SqlMapConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); factory = new SqlSessionFactoryBuilder().build(inputStream); } @Test public void testFindById(){ SqlSession session = factory.openSession(); User user = session.selectOne("cn.my.mybatis.entity.User.findById", 1); session.close(); System.out.println(user); } /** * 新增用户 */ @Test public void testInserUser(){ SqlSession session = factory.openSession(); User user = new User(); user.setAddress("许昌"); user.setUsername("曹操"); user.setSex("男"); user.setBirthday(new Date()); session.insert("cn.my.mybatis.entity.User.insertUser", user); session.commit();//这行记住一定不要忘,要不然提交不了 session.close(); System.out.println(user); } }
运行结果:
这样添加就写完了。如果需要添加完毕之后返回新增的id值。
user.xml代码
<!-- 新增映射 --> <insert id="insertUser" parameterType="cn.my.mybatis.entity.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>
测试类代码
/** * 新增用户 */ @Test public void testInserUser(){ SqlSession session = factory.openSession(); User user = new User(); user.setAddress("许昌"); user.setUsername("曹操1"); user.setSex("男"); user.setBirthday(new Date()); session.insert("cn.my.mybatis.entity.User.insertUser", user); session.commit(); session.close(); System.out.println(user.getId()); }
看结果:
2.修改
1.1 user.xml添加代码
<!-- 修改用户 --> <update id="updateUser" parameterType="cn.my.mybatis.entity.User"> UPDATE `user` SET `username` = #{username}, `birthday` = #{birthday}, `sex` = #{sex}, `address` = #{address} WHERE `id` = #{id} ; </update>
2.1测试类代码
/** * 修改 */ @Test public void testUpdateUser(){ SqlSession session = factory.openSession(); User user = new User(); user.setId(29); user.setAddress("许昌1"); user.setUsername("曹操2"); user.setSex("女"); user.setBirthday(new Date()); session.update("cn.my.mybatis.entity.User.updateUser", user); session.commit(); session.close(); }
运行结果:
3.删除
user.xml
<delete id="deleteUser" parameterType="java.lang.Integer"> delete from user where id=#{id} </delete>
测试类代码
/** * 删除 */ @Test public void testDeleteUser(){ SqlSession session = factory.openSession(); session.delete("cn.my.mybatis.entity.User.deleteUser", 29); session.commit(); session.close(); }
运行结构:删除id为29的。
好了以上就是一个最简单的例子。有时间我会继续往下写的,如果需要加群一起研究的可以q我,939705214,也可以添加群号581591235
时间: 2024-10-11 03:20:05