SSM:Mybatis实现增删改查

一,项目目录

SSM:Mybatis实现增删改查_第1张图片       

二,配置文件-mybatis-config.xml




	
			
	
	
		
			
				
				
					
					
				
			
		
	

		
		
	


三,基本类-Role.java

package Bean;

public class Role {	//类
	private int id;
	private String roleName;
	private String note;

	
	public Role() {
		super();
	}
	
	public Role(int id, String roleName, String note) {
		super();
		this.id = id;
		this.roleName = roleName;
		this.note = note;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

}

四,SqlSession获取类

package Util;

import java.io.*;
import org.apache.ibatis.io.*;
import org.apache.ibatis.session.*;

public class SqlSessionFactoryUtils {
	private final static Class LOCK = SqlSessionFactoryUtils.class;
	private static SqlSessionFactory sqlSessionFactory = null;

	private SqlSessionFactoryUtils() {	//构造函数
		
	}

	public static SqlSessionFactory getSqlSessionFactory() { // 创建SqlSessionFactory
		synchronized (LOCK) { // 加锁,为了防止多线程中多次实例化SqlSessionFactory对象,保证唯一性
			if (sqlSessionFactory != null) {
				return sqlSessionFactory;
			}
			String resource = "mybatis-config.xml";
			InputStream inputStream;
			try {
				inputStream = Resources.getResourceAsStream(resource); // 得到配置文件数据
				sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 通过SqlSessionFactoryBuilder的Builder创建SqlSessionFactory
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}
			return sqlSessionFactory;
		}

	}

	public static SqlSession openSqlSession() { // 创建SqlSession
		if (sqlSessionFactory == null) {
			getSqlSessionFactory();
		}
		return sqlSessionFactory.openSession(true); // SqlSessionFactory的openSession创建openSqlSession
	}
}

五,Dao层操作-Map包

package Map;

import java.util.List;

import Bean.Role;

public interface RoleMapper {	//接口
	public int insertRole(Role role);	//添加

	public int deleteRole(int id);	//删除

	public int updateRole(Role role);	//更新
	
	public Role getRole(int id);	//查询

	public List findRoles(String roleName);	//模糊查询

}


	
	insert into
		role_table(id,name,note)
		values(#{id},#{roleName},#{note})
	
	delete
		from role_table
		where
		id=#{id}
	
	
	update
		role_table set
		name=#{roleName},note=#{note} where id=#{id}
	
		
	

六,操作

package Util;

import java.util.ArrayList;

import org.apache.ibatis.session.SqlSession;
import Bean.Role;
import Map.RoleMapper;

public class Main {
	public static void main(String[] args) {
		SqlSession sqlSession = null;
		try {
			sqlSession = SqlSessionFactoryUtils.openSqlSession();
			RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);

			 Role role = roleMapper.getRole(1); //单查询
			 System.out.println(role.getRoleName());

			 ArrayList a=(ArrayList) roleMapper.findRoles("name"); //模糊查询
			 for(int i=0;i

 

你可能感兴趣的:(SSM/SpringBoot)