Mybatis实现简单的CURD操作

1.项目架构

Mybatis实现简单的CURD操作_第1张图片
需要的jar包
Mybatis实现简单的CURD操作_第2张图片
com.pojo.Grade.java(plain ordinary java object)实体类

package com.pojo;
public class Grade {
	private int id;
	private int grade;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getGrade() {
		return grade;
	}
	public void setGrade(int grade) {
		this.grade = grade;
	}
	@Override
	public String toString() {
		return "Grade [id=" + id + ", grade=" + grade + "]";
	}
}

log4j.properties日志输出文件

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

mysql.properties 数据库配置信息文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/examsystem?characterEcoding=utf-8
jdbc.username=root
jdbc.password=123456

SqlMapConfig.xml 主配置文件
通过引入外部数据库配置信息,使用${jdbc.driver}来获取值(注意:引入外部配置信息一定要写在最上面!否则configuration标签会报红出错)




	
	

	
	
		
			
			
			
			
				
				
				
				
			
		
	
	
	
		
	

DBUtil工具类
主要为了获取sqlSession
通过 MyBatis 环境等配置信息构造 SqlSessionFactory 即会话工厂,
由会话工厂创建 SQLSession 即会话,操作数据库需要通过 SQLSession 进行

package com.util;
import java.io.IOException;
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 DBUtil {
	private static SqlSessionFactory ssf;
	static {
		SqlSessionFactoryBuilder sfb =new SqlSessionFactoryBuilder();
		try {
			ssf=sfb.build(Resources.getResourceAsStream("SqlMapConfig.xml"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static SqlSession openSqlSession() {
		return ssf.openSession();
	}
}

Grade.xml
sql映射文件,配置了操作数据库的 SQL 语句。务必将对应接口类的全路径配置正确。并且id一定要唯一。





	
	
		 insert into Grade(grade) VALUES (#{grade})
	
	
		update Grade set grade=#{grade} where id= #{id}
	
	
		delete from Grade where id=#{id}
	

GradeMapper.java
sql映射文件对应的接口类

接口类与映射文件的对应
返回值类型 : resultType
方法名 : id
参数 :parameterType

package com.mapper;
import com.pojo.Grade;
public interface GradeMapper {
	public Grade selectById(int id); 
	public void insertGrade(int grade);
	public void updateGradeById(Grade grade);
	public void deleteGradeById(int id);
}

测试类:MybatisDemo.java

package com.test;

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

import com.mapper.GradeMapper;
import com.pojo.Grade;
import com.util.DBUtil;

public class MybatisDemo {
	//基于DBUtil和Mapper接口方式
	@Test	//查询
	public void testQuery2() {
		//1.获取SqlSession
		SqlSession ss=DBUtil.openSqlSession();
		//2.通过SqlSession的getMapper方法获取GradeMapper
		GradeMapper gm=ss.getMapper(GradeMapper.class);
		Grade grade=gm.selectById(1);
		System.out.println(grade);
		ss.close();
	}
	
	@Test	//插入
	public void testQuery3() {
		//1.获取SqlSession
		SqlSession ss=DBUtil.openSqlSession();
		//2.通过SqlSession的getMapper方法获取GradeMapper
		GradeMapper gm=ss.getMapper(GradeMapper.class);
		gm.insertGrade(6666);
		ss.commit();
		ss.close();
	}
	
	@Test	//删除
	public void testQuery4() {
		//1.获取SqlSession
		SqlSession ss=DBUtil.openSqlSession();
		//2.通过SqlSession的getMapper方法获取GradeMapper
		GradeMapper gm=ss.getMapper(GradeMapper.class);
		gm.deleteGradeById(2);
		ss.commit();
		ss.close();
	}
	
	@Test	//修改
	public void testQuery5() {
		//1.获取SqlSession
		SqlSession ss=DBUtil.openSqlSession();
		//2.通过SqlSession的getMapper方法获取GradeMapper
		GradeMapper gm=ss.getMapper(GradeMapper.class);
		Grade grade=new Grade();
		grade.setId(4);
		grade.setGrade(6666);
		gm.updateGradeById(grade);
		ss.commit();
		ss.close();
	}
	
}

提醒:如果在映射文件Grade.xml中,id务必唯一,且所有的sql语句不能存在错误。假设insert语句存在错误,但是执行的select语句是正确的,此时仍然会报错!

你可能感兴趣的:(Mybatis)