一、MyBatis的插入数据操作
1. 编写UserMapper映射文件
<?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="userMapper"> <!--查询操作--> <select id="findAll" resultType="com.itheima.domain.User"> select * from user </select> <!--插入操作--> <insert id="add" parameterType="com.itheima.domain.User"> insert into user values(#{id},#{username},#{password}) </insert> </mapper>
2. 编写插入实体User的代码
package com.itheima.test; import com.itheima.domain.User; 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 java.io.IOException; import java.io.InputStream; import java.util.List; public class MyBatisTest { @Test public void test2() throws IOException { //模拟user对象 User user = new User(); user.setUsername("tom"); user.setPassword("abc"); InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); int insert = sqlSession.insert("userMapper.add", user); System.out.println(insert); //mybatis执行更新操作 提交事务 sqlSession.commit(); sqlSession.close(); } }
执行test2,检查插入成功:
3. 插入操作注意问题
- 插入语句使用insert标签
- 在映射文件中使用parameterType属性指定要插入的数据类型
- Sql语句中使用#{实体属性名}方式引用实体中的属性值
- 插入操作使用的API是sqlSession.insert(“命名空间.id”,实体对象);
- 插入操作涉及数据库数据变化,所以要使用sqlSession对象显示的提交事务,即sqlSession.commit()
二、MyBatis的修改数据操作
1. 编写UserMapper映射文件
<?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="userMapper"> <!--查询操作--> <select id="findAll" resultType="com.itheima.domain.User"> select * from user </select> <!--插入操作--> <insert id="add" parameterType="com.itheima.domain.User"> insert into user values(#{id},#{username},#{password}) </insert> <!--修改操作--> <update id="update" parameterType="com.itheima.domain.User"> update user set username=#{username},password=#{password} where id=#{id} </update> </mapper>
2. 编写修改实体User的代码
package com.itheima.test; import com.itheima.domain.User; 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 java.io.IOException; import java.io.InputStream; import java.util.List; public class MyBatisTest { @Test //修改操作 public void test3() throws IOException { //模拟user对象 User user = new User(); user.setId(6); user.setUsername("lucy"); user.setPassword("123"); InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); int update = sqlSession.update("userMapper.update", user); System.out.println(update); sqlSession.commit(); sqlSession.close(); } }
执行test3,检查结果:
3. 修改操作注意问题
- 修改语句使用update标签
- 修改操作使用的API是sqlSession.update(“命名空间.id”,实体对象);
三、MyBatis的删除数据操作
1. 编写UserMapper映射文件
<?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="userMapper"> <!--查询操作--> <select id="findAll" resultType="com.itheima.domain.User"> select * from user </select> <!--插入操作--> <insert id="add" parameterType="com.itheima.domain.User"> insert into user values(#{id},#{username},#{password}) </insert> <!--修改操作--> <update id="update" parameterType="com.itheima.domain.User"> update user set username=#{username},password=#{password} where id=#{id} </update> <!--删除操作 #{}里面随便写--> <delete id="delete" parameterType="java.lang.Integer"> delete from user where id=#{id} </delete> </mapper>
2. 编写删除数据的代码
package com.itheima.test; import com.itheima.domain.User; 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 java.io.IOException; import java.io.InputStream; import java.util.List; public class MyBatisTest { @Test //删除操作 public void test4() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); int delete = sqlSession.delete("userMapper.delete", 6); System.out.println(delete); sqlSession.commit(); sqlSession.close(); } }
执行test4,检查结果:
3. 删除操作注意问题
- 删除语句使用delete标签
- Sql语句中使用#{任意字符串}方式引用传递的单个参数,根据多个条件删除时则传递的仍旧是User对象,那么parameterType=User
- 删除操作使用的API是sqlSession.delete(“命名空间.id”,Object);
四、知识小结
标签:改查,ibatis,apache,sqlSession,Mybatis20,user,org,Mybatis,import From: https://www.cnblogs.com/ajing2018/p/17450455.html