首页 > 代码库 > mybatis 的简单使用

mybatis 的简单使用

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

mysql-connector-java-5.1.6-bin

mybatis-3.2.2

先看项目目录:


配置文件mybatisconfig.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>
	<!-- 用XML映射的时候必须配此项!指定mappers里面的返回值对应的实体类 -->
	<!-- <typeAliases> 
        <typeAlias alias="User" type="com.miquan.mybatis.bean.User"/> 
    </typeAliases> -->
    <!-- JDBC配置 -->
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC"/>
			<dataSource type="POOLED">
				<property name="driver" value=http://www.mamicode.com/"com.mysql.jdbc.Driver"/>>
实体类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:

<?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">
<!-- namespace对应着接口类 -->
<mapper namespace="com.miquan.mybatis.inter.IUserOperation">
	<!-- <select id="selectById" parameterType="int" resultType="User">
		select * from User where id = #{id}
	</select> -->
</mapper>

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<User> 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<User> 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();
			}
        }
	}
}


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