标签:result apache turn order use HERE ide 类型 事务
利用mybatis实现简单的增删改查,一些重点的地方
UserDaoImpl.java
package com.dao.Impl; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import com.dao.UserDao; import com.pojo.User; public class UserDaoImpl implements UserDao { // 用它获取SQLSession对象(相当于connection) private SqlSessionFactory sqlSessionFactory; public UserDaoImpl(SqlSessionFactory sqlSessionFactory) { super(); this.sqlSessionFactory = sqlSessionFactory; } //插入,更新,删除都要提交事务 @Override public int saveUser(User user) { // TODO Auto-generated method stub SqlSession session=sqlSessionFactory.openSession(); int result=-1; try { //第一个参数是名称空间.id result=session.insert("com.pojo.User.saveUser",user); session.commit(); } finally { // TODO: handle finally clause session.close(); } return result; } @Override public int deleteUserById(Integer id) { // TODO Auto-generated method stub SqlSession session=sqlSessionFactory.openSession(); int result=-1; try { //第一个参数是名称空间.id result=session.delete("com.pojo.User.deleteUserById",id); session.commit(); } finally { // TODO: handle finally clause session.close(); } return result; } @Override public int update(User user) { // TODO Auto-generated method stub SqlSession session=sqlSessionFactory.openSession(); int result=-1; try { //第一个参数是名称空间.id result=session.delete("com.pojo.User.updateUser",user); session.commit(); } finally { // TODO: handle finally clause session.close(); } return result; } @Override // public User queryUserById(Integer id) { // TODO Auto-generated method stub SqlSession session=sqlSessionFactory.openSession(); try { //第一个参数是名称空间.id return session.selectOne("com.pojo.User.queryUserById",id); } finally { // TODO: handle finally clause session.close(); } } @Override public List<User> queryUsers() { // TODO Auto-generated method stub SqlSession session=sqlSessionFactory.openSession(); try { //第一个参数是名称空间.id return session.selectList("com.pojo.User.queryUsers"); } finally { // TODO: handle finally clause session.close(); } } }
UserMapper.xml
<?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="com.pojo.User"> <!-- public int saveUser(User user); insert标签配置insert语句 id属性配置唯一的标识 parameterType 设置方法的参数类型(可以省略,一般如果是JavaBean,不推荐省略) useGeneratedKeys="true" 表示使用数据库生所的主键 keyProperty="id" 属性设置将数据库中返回的自增id值交给哪个属性 --> <insert id="saveUser" parameterType="com.pojo.User"> <!-- order属性设置selectKey里配置的sql语句的执行顺序 AFTER 在insert语句之后执行 BEFORE 在insert语句之前执行 keyProperty="id" 属性设置将数据库中返回的自增id值交给哪个属性 resultType="int" 属性表示查询之后返回的类型 int 表示Integer类型 --> <selectKey order="AFTER" keyProperty="id" resultType="int"> select last_insert_id() </selectKey> insert into t_user(`last_name`,`sex`) values(#{lastName},#{sex}) </insert> <!-- public int deleteUserById(Integer id); --> <delete id="deleteUserById"> delete from t_user where id = #{id} </delete> <!-- public int updateUser(User user); --> <update id="update" parameterType="com.pojo.User"> update t_user set last_name=#{lastName},sex=#{sex} where id = #{id} </update> <!-- select 标签配置select查询语句 id 属性给当前的sql语句配置一个唯一标识 resultType 属性设置查询结果每行记录对应生成的JavaBean的全类名 parameterType 设置方法的参数类型(可以省略) --> <select id="queryUserById" parameterType="int" resultType="com.pojo.User"> select id,last_name lastName,sex from t_user where id = #{id} </select> <!-- public List<User> queryUsers(); --> <select id="queryUsers" resultType="com.pojo.User"> select id,last_name lastName,sex from t_user </select> </mapper>
标签:result apache turn order use HERE ide 类型 事务
原文地址:https://www.cnblogs.com/ywqtro/p/12242661.html