映射文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 <insert id ="insertUser" parameterType ="com.iot.mybatis.po.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 > <delete id ="deleteUser" parameterType ="java.lang.Integer" > delete from user where id=#{id} </delete > <update id ="updateUser" parameterType ="com.iot.mybatis.po.User" > update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id} </update >
(注:这里的birthday
字段在mysql表中是DATE类型,在User类中birthday
属性是java的java.util.Date
类型,并没有进行转换就插入成功了。
看到有的文章说,在字段中有Date和DateTime类型,在插入数据时只要将实体的属性设置成Timestamp就会对应mysql的DateTime类型,Date会对应mysql的Date类型:#{modified_date,jdbcType=TIMESTAMP}、#{date,jdbcType=DATE}
我上面的birthday
,配置成#{birthday,jdbcType=TIMESTAMP}
,结果也插入成功了,具体实现待查)
程序代码
User.java,在入门程序一基础上增加三个测试方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 @Test public void insertUserTest () throws IOException { String resource = "SqlMapConfig.xml" ; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder () .build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); User user = new User (); user.setUsername("王小军" ); user.setBirthday(new Date ()); user.setSex("1" ); user.setAddress("河南郑州" ); sqlSession.insert("test.insertUser" , user); sqlSession.commit(); System.out.println(user.getId()); sqlSession.close(); } @Test public void deleteUserTest () throws IOException { String resource = "SqlMapConfig.xml" ; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder () .build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); sqlSession.delete("test.deleteUser" , 29 ); sqlSession.commit(); sqlSession.close(); } @Test public void updateUserTest () throws IOException { String resource = "SqlMapConfig.xml" ; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder () .build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); User user = new User (); user.setId(27 ); user.setUsername("王大军" ); user.setBirthday(new Date ()); user.setSex("2" ); user.setAddress("河南郑州" ); sqlSession.update("test.updateUser" , user); sqlSession.commit(); sqlSession.close(); }
1 2 3 <selectKey keyProperty ="id" order ="AFTER" resultType ="java.lang.Integer" > SELECT LAST_INSERT_ID() </selectKey >
如果没有在上面的配置中配置resultType
,则会报下面的异常
1 2 3 4 5 6 7 8 9 10 11 12 13 org.apache.ibatis.exceptions.PersistenceException: ### Error updating database. Cause: org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped Statement 'test.insertUser!selectKey'. It's likely that neither a Result Type nor a Result Map was specified. ### The error may exist in sqlmap/User.xml ### The error may involve test.insertUser!selectKey-Inline ### The error occurred while setting parameters ### SQL: SELECT LAST_INSERT_ID() ### Cause: org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped Statement 'test.insertUser!selectKey'. It's likely that neither a Result Type nor a Result Map was specified. ... Caused by: org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped Statement 'test.insertUser!selectKey'. It's likely that neither a Result Type nor a Result Map was specified.
总结
#{}
表示一个占位符号,#{}
接收输入参数,类型可以是简单类型,pojo、hashmap。
如果接收简单类型,#{}
中可以写成value或其它名称。
#{}
接收pojo对象值,通过OGNL读取对象中的属性值,通过属性.属性.属性…的方式获取对象属性值。
${}
表示一个拼接符号,会引用sql注入,所以**不建议使用${}
**。
${}
接收输入参数,类型可以是简单类型,pojo、hashmap。
如果接收简单类型,${}
中只能写成value。
${}
接收pojo对象值,通过OGNL读取对象中的属性值,通过属性.属性.属性…的方式获取对象属性值。
mybatis和hibernate本质区别和应用场景
是一个标准ORM框架(对象关系映射)。入门门槛较高的,不需要程序写sql,sql语句自动生成了。对sql语句进行优化、修改比较困难的。
应用场景:适用与需求变化不多的中小型项目,比如:后台管理系统,erp、orm、oa。。
专注是sql本身,需要程序员自己编写sql语句,sql修改、优化比较方便。mybatis是一个不完全的ORM框架,虽然程序员自己写sql,mybatis也可以实现映射(输入映射、输出映射)。
应用场景:适用与需求变化较多的项目,比如:互联网项目。
企业进行技术选型,以低成本高回报作为技术选型的原则,根据项目组的技术力量进行选择。