<!-- 添加用户 parameterType:指定输入参数类型是pojo(包括用户信息) #{}中指定POJO的属性名,接收到POJO对象的属性值,mybatis通过OGNL获取对象的属性 --> <insert id="insertUser" parameterType="cn.edu.hpu.mybatis.PO.User"> insert into user(username,birthday,sex,address) value(#{username}.#{birthday,jdbcType=DATE}.#{sex},#{address}) </insert>
//添加用户 @Test public void insertUserTest(){ //mybatis配置文件 String resource="SqlMapConfig.xml"; //将配置文件加载成流 InputStream inputStream; try { inputStream = Resources.getResourceAsStream(resource); //创建会话工厂,传入mybatis配置文件的信息 SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); //通过工厂得到sqlSession sqlSession=sqlSessionFactory.openSession(); //插入用户对象 User user=new User(); user.setUsername("李云华"); user.setBirthday(new Date()); user.setSex("男"); user.setAddress("云南大理"); //通过SqlSession操作数据库 //第一个参数:映射文件中的statement的Id,等于=namespace+"."+statement的Id //第二个参数:指定和映射文件所匹配的parameterType类型的参数 //sqlSession.selectOne最终结果与你映射文件中所匹配的resultType类型 sqlSession.insert("test.insertUser",user); //提交事务 sqlSession.commit(); } catch (IOException e) { e.printStackTrace(); }finally{ //释放资源 sqlSession.close(); } }
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter. DEBUG [main] - PooledDataSource forcefully closed/removed all connections. DEBUG [main] - PooledDataSource forcefully closed/removed all connections. DEBUG [main] - PooledDataSource forcefully closed/removed all connections. DEBUG [main] - PooledDataSource forcefully closed/removed all connections. DEBUG [main] - Opening JDBC Connection DEBUG [main] - Created connection 30685694. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@1d439fe] DEBUG [main] - ==> Preparing: insert into user(username,birthday,sex,address) value(?,?,?,?) DEBUG [main] - ==> Parameters: 李云华(String), 2015-06-07(Date), 男(String), 云南大理(String) DEBUG [main] - <== Updates: 1 DEBUG [main] - Committing JDBC Connection [com.mysql.jdbc.Connection@1d439fe] DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.Connection@1d439fe] DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.Connection@1d439fe] DEBUG [main] - Returned connection 30685694 to pool.
<!-- 添加用户 parameterType:指定输入参数类型是pojo(包括用户信息) #{}中指定POJO的属性名,接收到POJO对象的属性值,mybatis通过OGNL获取对象的属性 --> <insert id="insertUser" parameterType="cn.edu.hpu.mybatis.PO.User"> <!-- 将插入数据的主键返回,返回到user对象中。 SELECT_INSERT_ID():得到刚insert进去的主键值,只适用于自增主键 KeyProperty:将查询到主键值设置到parameterType指定对象的哪个属性。 order:SELECT LAST_INSERT_ID()执行顺序,相对于insert语句来说它的执行顺序 --> <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> SELECT LAST_INSERT_ID() </selectKey> insert into user(username,birthday,sex,address) value(#{username},#{birthday,jdbcType=DATE},#{sex},#{address}) </insert>
//插入用户对象 User user=new User(); user.setUsername("李云华"); user.setBirthday(new Date()); user.setSex("男"); user.setAddress("云南大理"); //通过SqlSession操作数据库 //第一个参数:映射文件中的statement的Id,等于=namespace+"."+statement的Id //第二个参数:指定和映射文件所匹配的parameterType类型的参数 //sqlSession.selectOne最终结果与你映射文件中所匹配的resultType类型 sqlSession.insert("test.insertUser",user); //提交事务 sqlSession.commit(); System.out.println(user.getId());
<!-- 使用MySql的UUID来生成主键 执行过程: 首先通过uuid()得到主键,将主键设置到user对象的id属性中 其次在insert执行时,从user对象中取出id属性值 --> <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String"> SELECT uuid() </selectKey> insert into user(id,birthday,sex,address) value(#{id},#{birthday,jdbcType=DATE},#{sex},#{address}) 如果使用的数据库是oracle那么通过oracle的序列生成主键写法: <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String"> SELECT 序列名.nextval() </selectKey> insert into user(id,username,birthday,sex,address) value(#{id},#{username},#{birthday,jdbcType=DATE},#{sex},#{address})
<!-- 删除用户 --> <delete id="deleteUser" parameterType="java.lang.Integer"> delete from user where id=#{id} </delete> <!-- 更新用户 分析: 需要传入用户的id,需要传入用户的更新信息. parameterType指定user对象,包括id和更新信息(注意:id必须存在) #{id}:从输入user对象中获取id属性值--> <update id="updateUser" parameterType="cn.edu.hpu.mybatis.PO.User"> update user set username=#{username},birthday=#{birthday,jdbcType=DATE},sex=#{sex},address=#{address} where id=#{id} </update>
//删除用户 @Test public void deleteUserTest(){ //mybatis配置文件 String resource="SqlMapConfig.xml"; //将配置文件加载成流 InputStream inputStream; try { inputStream = Resources.getResourceAsStream(resource); //创建会话工厂,传入mybatis配置文件的信息 SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); //通过工厂得到sqlSession sqlSession=sqlSessionFactory.openSession(); //传入id删除用户 sqlSession.delete("test.deleteUser",6); //提交事务 sqlSession.commit(); } catch (IOException e) { e.printStackTrace(); }finally{ //释放资源 sqlSession.close(); } }
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter. DEBUG [main] - PooledDataSource forcefully closed/removed all connections. DEBUG [main] - PooledDataSource forcefully closed/removed all connections. DEBUG [main] - PooledDataSource forcefully closed/removed all connections. DEBUG [main] - PooledDataSource forcefully closed/removed all connections. DEBUG [main] - Opening JDBC Connection DEBUG [main] - Created connection 30685694. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@1d439fe] DEBUG [main] - ==> Preparing: delete from user where id=? DEBUG [main] - ==> Parameters: 6(Integer) DEBUG [main] - <== Updates: 1 DEBUG [main] - Committing JDBC Connection [com.mysql.jdbc.Connection@1d439fe] DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.Connection@1d439fe] DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.Connection@1d439fe] DEBUG [main] - Returned connection 30685694 to pool.
//更新用户 @Test public void updateUserTest(){ //mybatis配置文件 String resource="SqlMapConfig.xml"; //将配置文件加载成流 InputStream inputStream; try { inputStream = Resources.getResourceAsStream(resource); //创建会话工厂,传入mybatis配置文件的信息 SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); //通过工厂得到sqlSession sqlSession=sqlSessionFactory.openSession(); //更新用户信息(更改id=5的用户数据) User user=new User(); user.setId(5); user.setUsername("刘三姐"); user.setBirthday(new Date()); user.setSex("女"); user.setAddress("云南大理"); sqlSession.update("test.updateUser",user); //提交事务 sqlSession.commit(); } catch (IOException e) { e.printStackTrace(); }finally{ //释放资源 sqlSession.close(); } }
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter. DEBUG [main] - PooledDataSource forcefully closed/removed all connections. DEBUG [main] - PooledDataSource forcefully closed/removed all connections. DEBUG [main] - PooledDataSource forcefully closed/removed all connections. DEBUG [main] - PooledDataSource forcefully closed/removed all connections. DEBUG [main] - Opening JDBC Connection DEBUG [main] - Created connection 30685694. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@1d439fe] DEBUG [main] - ==> Preparing: update user set username=?,birthday=?,sex=?,address=? where id=? DEBUG [main] - ==> Parameters: 刘三姐(String), 2015-06-07(Date), 女(String), 云南大理(String), 5(Integer) DEBUG [main] - <== Updates: 1 DEBUG [main] - Committing JDBC Connection [com.mysql.jdbc.Connection@1d439fe] DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.Connection@1d439fe] DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.Connection@1d439fe] DEBUG [main] - Returned connection 30685694 to pool.
企业进行技术选型,以低成本 高回报作为技术选型的原则,根据项目组的技术力量进行选择。
转载请注明出处:http://blog.csdn.net/acmman/article/details/46455411