Eclipse使用MyBatis实现增删查改

想使用MyBatis实现增删查改功能,首先需要下载MyBatis和配置MyBatis,这两步具体怎么做可以去看我之前发过博客。

src的目录结构:
Eclipse使用MyBatis实现增删查改_第1张图片

UserMapper.java:
Eclipse使用MyBatis实现增删查改_第2张图片

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.gx.dao.UserMapper">
  <resultMap id="BaseResultMap" type="com.gx.pojo.User">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="birthday" jdbcType="TIMESTAMP" property="birthday" />
    <result column="sex" jdbcType="CHAR" property="sex" />
    <result column="address" jdbcType="VARCHAR" property="address" />
  </resultMap>
<sql id="Base_Column_List">
    id, username, birthday, sex, address
  </sql>

  <select id="SelectAll" resultMap="BaseResultMap">
  	select * from user
  </select>

<insert id="insert" parameterType="com.gx.pojo.User">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into user (username, birthday, sex, address)
    values (#{username,jdbcType=VARCHAR}, #{birthday,jdbcType=TIMESTAMP}, #{sex,jdbcType=CHAR},  #{address,jdbcType=VARCHAR})
  </insert>

  <update id="updateByPrimaryKey" parameterType="com.gx.pojo.User">
    update user
    set username = #{username,jdbcType=VARCHAR},
      	birthday = #{birthday,jdbcType=TIMESTAMP},
      	sex = #{sex,jdbcType=CHAR},
      	address = #{address,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from user
    where id = #{id,jdbcType=INTEGER}
  </delete>
</mapper>

User.java:
再生成get和 set(右键source/Generate Getters and Setters)和tostring方法(右键source/ Generate toString)
Eclipse使用MyBatis实现增删查改_第3张图片

MybatiesUtil.java:
由于增删查改等功能都需要这三步,为了不让代码重复过多,所以把这三步写在封装类里

private static SqlSessionFactory factory;
	static {
		try {
			//1、读取配置文件
			Reader reader =Resources.getResourceAsReader("mybatis-config.xml");
			//2、创建SqlSessionFactory工厂
			factory = new SqlSessionFactoryBuilder().build(reader);
			//factory = new SqlSessionFactoryBuilder().build(reader,"product");可以指定其他环境
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	public static SqlSession getSqlSession() {
		//3、使用工厂生产SqlSession对象
		return factory.openSession();
	}

(测试类)DemoMybaties.java:

public class DemoMybaties {
	static SqlSession sqlSession = null;
	@Test//查询 
	  public void testfun7() throws IOException {
	  sqlSession = MybatiesUtil.getSqlSession();
	  //获取dao的代理对象
	  UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
	  List<User> users=userMapper.SelectAll();//查询全部
	  
	  sqlSession.commit();//提交事务
	  sqlSession.close();//释放资源
	  }
	@Test // 新增
	public void testfun2() throws IOException, ParseException {
		sqlSession = MybatiesUtil.getSqlSession();
		
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    Date date = sdf.parse("1999-03-03 10:03:30");   
		//获取dao的代理对象
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		User user = new User(null, "张三", date, "男", "广东省");
		userMapper.insert(user);

		sqlSession.commit();//提交事务
		sqlSession.close();//释放资源
	}
	
	@Test // 修改
	public void testfun3() throws IOException, ParseException {
		sqlSession = MybatiesUtil.getSqlSession();
		
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    Date date = sdf.parse("1999-06-06 10:06:30");   

		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		User user = new User(57, "李四", date, "男", "广东省"); 
		int i = userMapper.updateByPrimaryKey(user); //根据ID修改
		//System.out.println(i);
		sqlSession.commit();//提交事务
		sqlSession.close();//释放资源

	}

	@Test //删除
	public void testfun4() throws IOException{
		sqlSession = MybatiesUtil.getSqlSession();
		//获取dao的代理对象
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		userMapper.deleteByPrimaryKey(53);//根据ID删除
		sqlSession.commit();//提交事务
		sqlSession.close();//释放资源

	}
}

你可能感兴趣的:(eclipse,java)