1.配置流程图

 

配置步骤:

  1.导入jar包

    

  2.创建总配置文件(文件名没有规定)

    

在scr目录先创建一个db.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/rbac-1227
jdbc.username=root
jdbc.password=1234



<?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> <properties resource="db.properties"/> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="cn/lzh/mybatis/mapper/UserMapper.xml"/> </mappers> </configuration>

  3.创建一个MybatisUtil工具类获得操作对象

package cn.lzh.mybatis.util;

import java.io.InputStream;

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

public class MybatisUtil {
	private MybatisUtil() {}
	//SqlSessionFactory 会话工厂对象
	private static SqlSessionFactory factory;
	//类加载到JVM中就立马执行static代码块,并且只会执行一次
	static {
		//资源文件
		String resource = "mybatis-config.xml";
		try(InputStream inputStream = Resources.getResourceAsStream(resource);) {
			//创建SqlSessionFactory对象
			factory = new SqlSessionFactoryBuilder().build(inputStream);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
	
	/**
	 * 创建session对象
	 * @return
	 */
	public static SqlSession oppenSession() {
		SqlSession session = factory.openSession();
		return session;
	}
}

  3.创建一个映射接口

package cn.lzh.mybatis.pojo;

public class User {
	private Integer id;
	private String name;
	private String perssion;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPerssion() {
		return perssion;
	}
	public void setPerssion(String perssion) {
		this.perssion = perssion;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", perssion=" + perssion + "]";
	}
	public User(Integer id, String name, String perssion) {
		super();
		this.id = id;
		this.name = name;
		this.perssion = perssion;
	}
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
}


package cn.lzh.mybatis.mapper;

import java.util.List;

import cn.lzh.mybatis.pojo.User;

public interface UserMapper {
	/**
	 * 增加数据
	 * @param user
	 * @return
	 */
	int insert(User user);
	
	/**
	 * 单行查询
	 * @param id
	 * @return
	 */
	User selectById(Integer id);
	
	/**
	 * 多行查询
	 * @return
	 */
	List<User> selectByAll();
	
	/**
	 * 修改
	 * @param user
	 * @return
	 */
	int updateUserInfo(User user);
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	int delete(Integer id);
}

  5.创建一个映射文件

<?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="cn.lzh.mybatis.mapper.UserMapper">
  
  <!-- 增加数据 -->
  <insert id="insert" parameterType="cn.lzh.mybatis.pojo.User" 
  	useGeneratedKeys="true" 
  	keyProperty="id"
  	keyColumn="id">
  	insert into user (name,perssion) values (#{name},#{perssion})
  </insert>
  
  <!-- 单行查询 -->
  <select id="selectById" parameterType="Integer" resultType="cn.lzh.mybatis.pojo.User">
  	select * from user where id = #{id}
  </select>
  
  <!-- 多行查询 -->
  <select id="selectByAll" resultType="cn.lzh.mybatis.pojo.User">
  	select * from user
  </select>
  
  <!-- 修改 -->
  <update id="updateUserInfo" parameterType="cn.lzh.mybatis.pojo.User">
  	update user set name = #{name} ,perssion = #{perssion} where id = #{id}
  </update>
  
  <!-- 删除 -->
  <delete id="delete" parameterType="Integer">
  	delete from user where id = #{id}
  </delete>
</mapper>

  6.在总配置文件加载映射文件

 <mappers>
    <mapper resource="cn/lzh/mybatis/mapper/UserMapper.xml"/>
  </mappers> 

  7.编写测试类

 

package cn.lzh.mybatis.test;

import static org.junit.Assert.*;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import cn.lzh.mybatis.mapper.UserMapper;
import cn.lzh.mybatis.util.MybatisUtil;
import cn.lzh.mybatis.pojo.User;

public class MybatisTest {
	
	@Test
	public void insert() throws Exception {
		//创建SqlSession操作对象
		SqlSession session = MybatisUtil.oppenSession();
		//创建userMapper接口代理对象
		UserMapper mapper = session.getMapper(UserMapper.class);
		//创建用户对象
		User user=new User(null, "lch", "男");
		//执行插入操作
		mapper.insert(user);
		//事务提交
		session.commit();
		//事务关闭
		session.close();
	}
	
	@Test
	public void selectById() throws Exception {
		//创建SqlSession的操作对象
		SqlSession session = MybatisUtil.oppenSession();
		//创建UserMapper接口的代理对象
		UserMapper userMapper = session.getMapper(UserMapper.class);
		//执行单行操作对象
		User selectById = userMapper.selectById(1);
		
		System.out.println(selectById);
		//关闭session
		session.close();
	}
	
	@Test
	public void selectByAll() throws Exception {
		//从创建SqlSession的操作对象
		SqlSession session = MybatisUtil.oppenSession();
		//创建UserMapper接口的代理操作对象
		UserMapper userMapper = session.getMapper(UserMapper.class);
		//执行多行操作
		List<User> users = userMapper.selectByAll();
		for (User user : users) {			
			System.out.println(user);
		}
		//关闭session
		session.close();
	}
	
	@Test
	public void updateUserInfo() throws Exception {
		//创建sqlsession的操作对象
		SqlSession session = MybatisUtil.oppenSession();
		//创建usermapper的代理操作对象
		UserMapper userMapper = session.getMapper(UserMapper.class);
		User user = new User(1, "lch","nan");
		//执行修改
		userMapper.updateUserInfo(user );
		//提交事务
		session.commit();
		//事务关闭
		session.close();
	}
	
	@Test
	public void delete() throws Exception {
		//创建sqlsession的操作对象
		SqlSession session = MybatisUtil.oppenSession();
		//创建usermapper接口的代理操作对象
		UserMapper userMapper = session.getMapper(UserMapper.class);
		//执行删除操作
		userMapper.delete(2);
		//事务提交
		session.commit();
		//事务关闭
		session.close();
		
	}
}

 

Mybatis基于注解的配置

  1.修改总配置文件加载的映射文件

 <mappers>
    <mapper class="cn.lzh.mybatis.mapper.UserMapper"/>
  </mappers>

  2.修改映射接口

package cn.lzh.mybatis.mapper;

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 cn.lzh.mybatis.pojo.User;

public interface UserMapper {
	/**
	 * 增加数据
	 * @param user
	 * @return
	 */
	@Insert("insert into user (name,perssion) values (#{name},#{perssion})")
	int insert(User user);
	
	/**
	 * 单行查询
	 * @param id
	 * @return
	 */
	@Select("select * from user where id = #{id}")
	User selectById(Integer id);
	
	/**
	 * 多行查询
	 * @return
	 */
	@Select("select * from user")
	List<User> selectByAll();
	
	/**
	 * 修改
	 * @param user
	 * @return
	 */
	@Update("update user set name = #{name},perssion = #{perssion} where id = #{id}")
	int updateUserInfo(User user);
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Delete("delete from user where id = #{id}")
	int delete(Integer id);
}