当前位置:Gxlcms > mysql > mybatis的简单使用

mybatis的简单使用

时间:2021-07-01 10:21:17 帮助过:51人阅读

需要用到的包:(这里只是其中一个版本,其他的百度) mysql-connector-java-5.1.6-bin mybatis-3.2.2 先看项目目录: 配置文件mybatisconfig.xml: ?xml version=1.0 encoding=UTF-8?!DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp:/

需要用到的包:(这里只是其中一个版本,其他的百度)

mysql-connector-java-5.1.6-bin

mybatis-3.2.2

先看项目目录:

\

配置文件mybatisconfig.xml:




	
	
    
	
		
			
			
				
				
				
				
			
		
	
	
	
		
	
实体类User.java:
package com.miquan.mybatis.bean;

public class User {
	private int id;
	private String userName;
	private String password;
	
	public User(int id, String userName, String password) {
		super();
		this.id = id;
		this.userName = userName;
		this.password = password;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}
User.xml:




	
IUserOperation.java:
package com.miquan.mybatis.inter;

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.miquan.mybatis.bean.User;

public interface IUserOperation {
	@Select("select * from User where id = #{id}")
	public User selectById(int id);
	
	@Select("select * from User where userName = #{userName}")
	public List selectUsers(String userName);
	
	@Insert("insert into User values(null, #{userName}, #{password})")
	public boolean addUser(User user); 
	
	@Delete("delete from User where id = #{id}")
	public boolean delUser(int id);
	
	@Update("update User "
			+ "set userName = #{userName}, "
				+ "password = #{password} "
			+ "where id = #{id}")
	public boolean updateUser(User user);
}

最后进行测试:Test.java:
package com.miquan.mybatis.test;

import java.io.InputStream;
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 com.miquan.mybatis.bean.User;
import com.miquan.mybatis.inter.IUserOperation;

public class Test {
	static SqlSession session;
	
	public static void main(String[] args) {
		try {
			//获取session
			InputStream is = Resources.getResourceAsStream("mybatisconfig.xml");
			SqlSessionFactory sqlSessionFactory = 
					new SqlSessionFactoryBuilder().build(is);
			session = sqlSessionFactory.openSession();
			
			//XML配置对应用法
//			User user = session.selectOne("com.miquan.mybatis.bean.UserDB.selectById", 1);
//			System.out.println(user.toString());
			
			//接口调用法
			//////注意:insert、delete和update方法调用之后必须commit才能真正的改变数据
			IUserOperation userOperation = session.getMapper(IUserOperation.class);
			
			//查
//			User user = userOperation.selectById(1);
//			System.out.println(user.toString());
			
			//查找List
//			List users = userOperation.selectUsers("miquan");
//			System.out.println(users.size());
			
			//增
//			User user = new User();
//			user.setUserName("zhiquan");
//			user.setPassword("999");
//			userOperation.addUser(user);
//			session.commit();
			
			//删
//			userOperation.delUser(7);
//			session.commit();
			
			//改
			User user = new User(8, "qiantu", "shaxppp");
			userOperation.updateUser(user);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				session.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
        }
	}
}

简单的增删查改可以实现了。

人气教程排行