首頁 > 資料庫 > mysql教程 > Mybatis简单的CRUD基于XML文件配置

Mybatis简单的CRUD基于XML文件配置

WBOY
發布: 2016-06-07 16:03:55
原創
1014 人瀏覽過

所有的ORM框架学习曲线都是先来一个CRUD爽一爽,下面我们就来CRUD一下,所有的配置都是基于上一篇的配置。废话不多说,直接上代码。 ?xml version=1.0 encoding=UTF-8 ?!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/m

所有的ORM框架学习曲线都是先来一个CRUD爽一爽,下面我们就来CRUD一下,所有的配置都是基于上一篇的配置。废话不多说,直接上代码。

<?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.bird.mybatis.bean.userMapper">
	<!--CRUD  -->
	<insert id="insertUser" parameterType="com.bird.mybatis.bean.Users">
		insert into users(name, age) values(#{name}, #{age});
	</insert>
	
	<delete id="deleteUser" parameterType="int">
		delete from users where id = #{id};
	</delete>
	
	<update id="updateUser" parameterType="com.bird.mybatis.bean.Users">
		update users set name = #{name}, age = #{age} where id = #{id};
	</update>
	
	<select id="getUser" parameterType="int" resultType="com.bird.mybatis.bean.Users">
		select * from users where id = #{id};
	</select>
	
	<select id="getAllUser" resultType="com.bird.mybatis.bean.Users">
		select * from users;
	</select>
</mapper> 
登入後複製
package com.bird.mybatis.bean;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

public class MyTest {
	
	private SqlSessionFactory factory;
	
	@Before
	public void init() {
		String resource = "conf.xml";
		Reader reader = null;
		try {
			reader = Resources.getResourceAsReader(resource);
		} catch (IOException e) {
			e.printStackTrace();
		}
		factory = new SqlSessionFactoryBuilder().build(reader);
	}
	
	@Test
	public void testAdd() {
		//默认是手动的 
		SqlSession session = factory.openSession();
		String statement = "com.bird.mybatis.bean.userMapper.insertUser";
		int rows = session.insert(statement, new Users(-1, "哈哈", 29));
		//提交
		session.commit();
		System.out.println(rows);
		session.close();
	}
	
	@Test
	public void testUpdate() {
		SqlSession session = factory.openSession();
		String statement = "com.bird.mybatis.bean.userMapper.updateUser";
		session.update(statement, new Users(4, "呵呵", 29));
		session.commit();
		session.close();
	}
	
	@Test
	public void testDelete() {
		SqlSession session = factory.openSession();
		String statement = "com.bird.mybatis.bean.userMapper.deleteUser";
		session.delete(statement, 1);
		session.commit();
		session.close();
	}
	
	@Test
	public void testGetAllUser() {
		SqlSession session = factory.openSession();
		String statement = "com.bird.mybatis.bean.userMapper.getAllUser";
		List<Users> list = session.selectList(statement);
		System.out.println(list.size());
		session.close();
	}
}
登入後複製
Ok,so easy, 哪里不会点哪里
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板