简单的MyBatis实例(2),基于Annotation注解实现CRUD,4个文件

基于annotation注解的版本,比上一篇只是做了细微改动,还是四个文件

下载地址:https://git.oschina.net/zhuhongliang/SimpleMybatisDemo/tree/MyBatisByAnnotation/

目录结构图:

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

数据库和上一篇的xml版一样:库名:test        表名:users     字段:int:id    String:name     int:age

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

 

下面看代码:User.java文件没有变动,其他三个文件均有细微变化,xml映射文件变成了UserMpaaerI.java接口文件,Test1.java 改为Test.java文件,conf.xml里的mapper注册文件修改

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 class="com.zhl.mapping.UserMapperI" />
    </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+"]";
	}
}




UserMapperI.java:


package com.zhl.mapping;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.zhl.obj.User;

public interface UserMapperI {

	
	@Insert("insert into users (name, age) values(#{name}, #{age})")
	public int addUser(User user);
	
	@Select("select * from users where id = #{id}")
	public User getUser(int id);
	
	@Select("select * from users")
	public List<User> getAll();
	
	@Update("update users set name= #{name}, age=#{age} where id= #{id}")
	public int updateUser (User user);
	
	@Delete("delete from users where name = #{name}")
	public int deleteUser(String name);
	
}




Test2.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.mapping.UserMapperI;
import com.zhl.obj.User;

public class Test2 {

	static String resource = "conf.xml";
	static InputStream is = Test2.class.getClassLoader().getResourceAsStream(resource);
	static SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
	
	public static void addUser(){
		SqlSession session = sessionFactory.openSession();
		UserMapperI mapper = session.getMapper(UserMapperI.class);
		User user = new User();
		user.setAge(88);
		user.setName("哈利波特");
		int result = mapper.addUser(user);
		session.commit();
		session.close();
		System.out.println("新增数据: "+result+" 条。");	
	}
	
	public static void getUser(){
		SqlSession session = sessionFactory.openSession();
		UserMapperI mapper = session.getMapper(UserMapperI.class);
		User user = mapper.getUser(1);
		session.close();
		System.out.println(user);
	}
	
	public static void getAll(){
		SqlSession session = sessionFactory.openSession();
		UserMapperI mapper = session.getMapper(UserMapperI.class);
		List<User> users = mapper.getAll();
		session.close();
		System.out.println(users);
	}
	
	public static void updateUser(int id,String name, int age){
		SqlSession session = sessionFactory.openSession();
		UserMapperI mapper = session.getMapper(UserMapperI.class);
		User user = new User();
		user.setAge(age);
		user.setId(id);
		user.setName(name);
		int result = mapper.updateUser(user);
		session.commit();
		session.close();
		System.out.println("更新数据: "+result+" 条。");
		
	}
	
	public static void deleteUser(){
		SqlSession session = sessionFactory.openSession();
		UserMapperI mapper = session.getMapper(UserMapperI.class);
		int result = mapper.deleteUser("哈利波特");
		session.commit();
		session.close();
		System.out.println("删除数据: "+result+" 条。");
		
		
	}
	
	public static void main(String[] args) {
		getAll();
		addUser();
		getAll();
		getUser();
		getAll();
		updateUser(13,"齐天大圣孙悟空",18000);
		getAll();
		updateUser(13,"平天大圣牛魔王",36000);
		getAll();
		deleteUser();
		getAll();
		
	}

}





运行Test.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= 42,name= 哈利波特,age= 88]]
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= 42,name= 哈利波特,age= 88]]
更新数据: 1 条。
[User [id= 1,name= 托尼斯塔克,age= 9], User [id= 9,name= 天王盖地虎,age= 20], User [id= 13,name= 齐天大圣孙悟空,age= 18000], User [id= 25,name= 天王盖地虎,age= 20], User [id= 42,name= 哈利波特,age= 88]]
更新数据: 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= 42,name= 哈利波特,age= 88]]
删除数据: 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/MyBatisByAnnotation/

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