简单的MyBatis实例(1),基于xml实现CRUD,4个文件

一共只有四个文件,实现增删改查。下面看图

源代码下载地址: https://git.oschina.net/zhuhongliang/SimpleMybatisDemo/tree/MyBatisByXml/

目录结构:

%e7%9b%ae%e5%bd%95%e7%bb%93%e6%9e%84

 

准备mysql 数据库 数据表 字段:库名:test        表名:users     字段:int:id    String:name     int:age

%e6%95%b0%e6%8d%ae%e5%ba%93

 

下面看代码:

conf.xml     配置文件文件代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <!-- 配置数据库连接信息 -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/test?userUnicode=true&characterEncoding=UTF-8" />
                <property name="username" value="root" />
                <property name="password" value="root" />
            </dataSource>
        </environment>
    </environments>
    
    <mappers>
    	<mapper resource="com/zhl/mapping/userMapper.xml"/>
    </mappers>
    
</configuration>



User.java    实体类文件代码:

package com.zhl.obj;

public class User {

	private int id;
	private String name;
	private int age;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	public String toString(){
		return "User [id= "+id+",name= "+name+",age= "+age+"]";
	}
	
}



userMapper.xml     sql映射文件代码:

<?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.zhl.mapping.userMapper">

	<!-- 查一个 -->
	<select id="getUser" parameterType="int" resultType="com.zhl.obj.User">
		select * from users where id=#{id}
	</select>
	
	<!-- 增 -->
	<insert id="addUser" parameterType="com.zhl.obj.User">
		insert into users(name, age) values(#{name}, #{age})
	</insert>
	
	<!-- 删 -->
	<delete id="deleteUser" parameterType="String">
		delete from users where name=#{name}
	</delete>
	
	<!-- 改 -->
	<update id="updateUser" parameterType="com.zhl.obj.User">
		update users set name=#{name}, age=#{age} where id=#{id}
	</update>

	<!-- 查全部 -->
	<select id="getAll"  resultType="com.zhl.obj.User">
		select * from users
	</select>

</mapper>



Test1.java      测试一下代码:


package com.zhl.test;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.zhl.obj.User;

public class Test1 {

	static String resource = "conf.xml";
	static InputStream is = Test1.class.getClassLoader().getResourceAsStream(resource);
	static SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
	
	
	public static void addUser(){
		User user = new User();
		user.setName("Jack Sparrow");
		user.setAge(800);
		
		SqlSession session = sessionFactory.openSession();
		String addUser = "com.zhl.mapping.userMapper.addUser";
		int result = session.insert(addUser, user);
		session.commit();		//一定要加上commit,因为默认不是自动提交的。
		session.close();
		System.out.println("增加数据: "+result+" 条。(增加杰克船长)");
		
	}
	
	public static void getUser(){
		
		SqlSession session = sessionFactory.openSession();
		String getUser = "com.zhl.mapping.userMapper.getUser";
		User user = session.selectOne(getUser, 1 );
		session.close();
		System.out.println(user);
	}
	
	public static void updateUser(String name, int age){
		User user = new User();
		user.setId(1);
		user.setName(name);
		user.setAge(age);
		
		SqlSession session = sessionFactory.openSession();
		String updateUser = "com.zhl.mapping.userMapper.updateUser";
		int result = session.update(updateUser, user);
		session.commit();		//一定要加上commit,因为默认不是自动提交的。
		session.close();
		System.out.println("更新数据: "+result+" 条。");
	}
	
	public static void deleteUser(){
		SqlSession session = sessionFactory.openSession();
		String deleteUser = "com.zhl.mapping.userMapper.deleteUser";
		int result = session.delete(deleteUser, "Jack Sparrow");
		session.commit();		//一定要加上commit,因为默认不是自动提交的。
		session.close();
		System.out.println("删除数据: "+result+" 条。(杰克船长被删除)");	
	}
	
	public static void getAll(){
		SqlSession session = sessionFactory.openSession();
		String getAll = "com.zhl.mapping.userMapper.getAll";
		List<User> userlist = session.selectList(getAll);
		System.out.println(userlist);
		
	}
	
	public static void main(String [] args){
		getAll();
		addUser();//增
		getAll();
		getUser();//查
		getAll();
		updateUser("川普", 18);//改
		getAll();
		updateUser("托尼斯塔克", 9);//改
		getAll();
		deleteUser();//删
		getAll();
	}
	
}


Test1.java的运行结果:

[User [id= 1,name= 托尼斯塔克,age= 9], User [id= 9,name= 天王盖地虎,age= 20], User [id= 13,name= 平天大圣牛魔王,age= 36000], User [id= 25,name= 天王盖地虎,age= 20]]
增加数据: 1 条。(增加杰克船长)
[User [id= 1,name= 托尼斯塔克,age= 9], User [id= 9,name= 天王盖地虎,age= 20], User [id= 13,name= 平天大圣牛魔王,age= 36000], User [id= 25,name= 天王盖地虎,age= 20], User [id= 40,name= Jack Sparrow,age= 800]]
User [id= 1,name= 托尼斯塔克,age= 9]
[User [id= 1,name= 托尼斯塔克,age= 9], User [id= 9,name= 天王盖地虎,age= 20], User [id= 13,name= 平天大圣牛魔王,age= 36000], User [id= 25,name= 天王盖地虎,age= 20], User [id= 40,name= Jack Sparrow,age= 800]]
更新数据: 1 条。
[User [id= 1,name= 川普,age= 18], User [id= 9,name= 天王盖地虎,age= 20], User [id= 13,name= 平天大圣牛魔王,age= 36000], User [id= 25,name= 天王盖地虎,age= 20], User [id= 40,name= Jack Sparrow,age= 800]]
更新数据: 1 条。
[User [id= 1,name= 托尼斯塔克,age= 9], User [id= 9,name= 天王盖地虎,age= 20], User [id= 13,name= 平天大圣牛魔王,age= 36000], User [id= 25,name= 天王盖地虎,age= 20], User [id= 40,name= Jack Sparrow,age= 800]]
删除数据: 1 条。(杰克船长被删除)
[User [id= 1,name= 托尼斯塔克,age= 9], User [id= 9,name= 天王盖地虎,age= 20], User [id= 13,name= 平天大圣牛魔王,age= 36000], User [id= 25,name= 天王盖地虎,age= 20]]

 

源代码下载地址:https://git.oschina.net/zhuhongliang/SimpleMybatisDemo/tree/MyBatisByXml/

 

 

要发表评论,您必须先登录