MyBatis - 02映射文件
作者:互联网
MyBatis - 02映射文件
(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"> //1. 映射文件DTD约束头 <mapper namespace="com.bearpx.spring.mybatis.dao.UserDao"> // 2. mapper:根标签 3. namespace:命名空间,与下面的id 共同组成查询标识 // 4. select:查询操作,还有insert、update、delete 5. resultType:查询结果对应的实体类型 6. findAll: 语句的id标识 <select id="findAll" resultType="com.bearpx.spring.mybatis.domain.User"> select * from user // 7. 要执行的sql语句 </select> </mapper>
找不到userMapper.update1: 命名空间+id 出现不匹配
Caused by: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for userMapper.update1 at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:888) at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:721) at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:714) at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:197)
(2)插入操作
<!-- 更新--> <insert id="save" parameterType="com.bearpx.spring.mybatis.domain.User"> insert into user values(#{id}, #{username}, #{password}) </insert>
> 插入语句使用insert标签,使用parameterType属性指定要插入的数据类型
> sql语句中使用 #{实体属性名} 方式引用实体中的属性值
> 插入操作使用的API 是 sqlSession.insert("命名空间.id", 实体对象)
> 插入操作涉及数据库数据变化,要使用sqlSession对象显示的提交事务,即 sqlSession.commit()
@Test public void test2() throws IOException { User user = new User(); user.setUsername("tomi"); user.setPassword("11111111"); // 获得核心配置文件 InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); // 获得session工厂对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); // 获得session 会话对象 SqlSession sqlSession = sqlSessionFactory.openSession(); // 执行操作 参数: namespace+id int result = sqlSession.insert("userMapper.save", user); System.out.println(result); // mybatis 执行更新操作 提交事务 sqlSession.commit(); // 释放资源 sqlSession.close(); }
(3)修改操作
<!-- 修改操作--> <update id="update" parameterType="com.bearpx.spring.mybatis.domain.User"> update user set username=#{username}, password=#{password} where id=#{id} </update>
@Test public void test3() throws IOException { User user = new User(); user.setId(3); user.setUsername("firefly"); user.setPassword("222222"); InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); sqlSession.update("userMapper.update", user); sqlSession.commit(); sqlSession.close(); }
(4)删除操作
<!--删除操作--> <!--<delete id="delete" parameterType="java.lang.Integer">--> <delete id="delete" parameterType="int"> delete from user where id = #{id} </delete>
@Test public void testDelete() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); int result = sqlSession.delete("userMapper.delete", 6); sqlSession.commit();
sqlSession.close(); }
(5)查询操作 findById
<select id="findById" resultType="user" parameterType="int"> select * from user where id=#{id} </select>
@Test public void testFindById() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); User user = sqlSession.selectOne("userMapper.findById", 1); System.out.println(user); sqlSession.close(); }
标签:02,sqlSessionFactory,映射,sqlSession,session,resourceAsStream,user,MyBatis,id 来源: https://www.cnblogs.com/kingdomer/p/14026888.html