Mybatis连接数据库常用语句整理汇总(1)

这期博客我将会重点回顾Mybatis常用的语法,包括基本的增删查改语句的书写,当然查询SQL我们会重点突出,包含的知识点也会比较多。

我们首先进行配置文件,新建db.properties,该文件主要用于数据库连接,其代码如下:

#MYSQL config
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://127.0.0.1:3306/db_mybatis?characterEncoding=utf-8
jdbc.username = root
jdbc.password = 123456

然后我们新建sqlMapConfig.xml文件,该文件主要用于①引入db.properties文件;②别名的映射;③应用POOLED方式连接数据源;④加载包含SQL语句的xml文件。其代码如下:




	
	
	
	
		
		
		
		
	
  
    
      
      
      
      
      
        
        
        
        
      
    
  
  
  
  	
    
  

然后我们引入log4j,mybatis以及mysql的jar包,这里我们为方便查看日志信息,新建log4j.properties,其代码如下:

# Global logging configuration
#\u751F\u4EA7\u73AF\u5883\u914D\u7F6Einfo   ERROR
log4j.rootLogger=DEBUG,stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# 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

首先是基本增删查改语句的书写,我们新建实体类User,其代码如下:

package cn.neu.mybatis.entity;

import java.util.Date;

public class User {
	
	private int uId;
	private String uName;
	private String uPwd;
	private String uPhone;
	private double uBalance;
	private int uState;
	private int uRole;
	private String uImage;//用户头像
	private Date uBirth;
	
	public int getuId() {
		return uId;
	}
	public void setuId(int uId) {
		this.uId = uId;
	}
	public String getuName() {
		return uName;
	}
	public void setuName(String uName) {
		this.uName = uName;
	}
	public String getuPwd() {
		return uPwd;
	}
	public void setuPwd(String uPwd) {
		this.uPwd = uPwd;
	}
	public String getuPhone() {
		return uPhone;
	}
	public void setuPhone(String uPhone) {
		this.uPhone = uPhone;
	}
	public double getuBalance() {
		return uBalance;
	}
	public void setuBalance(double uBalance) {
		this.uBalance = uBalance;
	}
	public int getuState() {
		return uState;
	}
	public void setuState(int uState) {
		this.uState = uState;
	}
	public int getuRole() {
		return uRole;
	}
	public void setuRole(int uRole) {
		this.uRole = uRole;
	}
	
	public String getuImage() {
		return uImage;
	}
	public void setuImage(String uImage) {
		this.uImage = uImage;
	}
	
	public Date getuBirth() {
		return uBirth;
	}
	public void setuBirth(Date uBirth) {
		this.uBirth = uBirth;
	}
	
	public User(int uId, String uName, String uPwd, String uPhone, double uBalance, int uState, int uRole,String uImage,Date uBirth) {
		super();
		this.uId = uId;
		this.uName = uName;
		this.uPwd = uPwd;
		this.uPhone = uPhone;
		this.uBalance = uBalance;
		this.uState = uState;
		this.uRole = uRole;
		this.uImage = uImage;
		this.uBirth = uBirth;
	}
	public User() {
		super();
	}
	public User(String uName, String uPwd, String uPhone) {
		super();
		this.uName = uName;
		this.uPwd = uPwd;
		this.uPhone = uPhone;
	}
	
	//添加注册信息
	public User(String uName, String uPwd, String uPhone, Date uBirth) {
		super();
		this.uName = uName;
		this.uPwd = uPwd;
		this.uPhone = uPhone;
		this.uBirth = uBirth;
	}
	
	public User(String uName, String uPwd, String uPhone, String uImage) {
		super();
		this.uName = uName;
		this.uPwd = uPwd;
		this.uPhone = uPhone;
		this.uImage = uImage;
	}
	
	public User(String uName, String uPwd) {
		super();
		this.uName = uName;
		this.uPwd = uPwd;
	}
	@Override
	public String toString() {
		return "User [uId=" + uId + ", uName=" + uName + ", uPwd=" + uPwd + ", uPhone=" + uPhone + ", uBalance="
				+ uBalance + ", uState=" + uState + ", uRole=" + uRole + ", uImage=" + uImage + ", uBirth=" + uBirth
				+ "]";
	}

}

然后我们新建UserMapper接口,里面主要包含未实现的方法,其代码如下:

package cn.neu.mybatis.mapper;

import java.util.List;
import java.util.Map;

import cn.neu.mybatis.entity.User;

public interface UserMapper {
	
	public User findUserByUid(int uId) throws Exception;
	
	public User findUserByUid2(int uId) throws Exception;
	
	public int deleteUserByUid(int uId) throws Exception;
	
	public int addUser(User user) throws Exception;
	
	public int updateUser(User user) throws Exception;
	
	//根据用户编号查询用户信息,根据用户类别查询用户信息,根据用户状态
	public List findUsersByConditions(User user) throws Exception;
	//返回值为Map的查询方法
	public List> findUserMap(User user) throws Exception;
	//查询foreach1
	public List foreachTest1(Object[] uIds) throws Exception;
	//查询foreach2
	public List foreachTest2(List uIds) throws Exception;
	
	public int updateUser1(User user) throws Exception;
}

然后我们在相同的mapper包下新建UserMapper.xml文件,该文件主要包含拼接的SQL语句,其代码如下:







	
	
	
	
	
	
		insert into tb_user(U_NAME,U_PWD,U_PHONE) values(#{uName},#{uPwd},#{uPhone})
	
	
	
	
		DELETE FROM tb_user
		WHERE U_ID = #{uId}
	
	
	
		UPDATE tb_user SET
		U_NAME=#{uName},U_PHONE=#{uPhone},U_IMAGE=#{uImage},U_PWD=#{uPwd}
		where U_ID = #{uId}
	

	
	
		SELECT U_ID uId,U_NAME uName,U_PWD uPwd,U_PHONE
		uPhone,U_BALANCE uBalance,U_STATE uState,U_ROLE uRole,U_IMAGE
		uImage,U_BIRTH uBirth FROM tb_user
	

	
	
	
		
			AND U_ID = #{uId}
		
		
			AND U_NAME = #{uName}
		
		
			AND U_PWD = #{uPwd}
		
	

	

	
	

	
	

	

	
		
			update tb_user
			
			
				
					U_NAME = #{uName},
				
				
					U_PWD = #{uPwd},
				
				
					U_PHONE = #{uPhone},
				
			
			where U_ID = #{uId}
		
	
  

然后我们进行单元测试,对每一个方法都要进行测试,新建的测试类名为UserMapperTest,其代码如下:

package cn.neu.mybatis.test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

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 org.junit.Before;
import org.junit.Test;

import cn.neu.mybatis.entity.User;
import cn.neu.mybatis.mapper.UserMapper;

public class UserMapperTest {
	
	private SqlSessionFactory sqlSessionFactory;
	
	@Before
	public void init() throws Exception {
		InputStream inputStream = Resources.getResourceAsStream("config/sqlMapConfig.xml");
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
	}

	@Test
	public void testFindUserByUid() throws Exception {
		//Spring可以使用自动装载,协助实例化UserMapper,直接调用即可
		SqlSession session = sqlSessionFactory.openSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		User user = userMapper.findUserByUid(2);
		System.out.println(user.toString());
		session.close();
	}
	
	@Test
	public void testDeleteUserByUid() throws Exception {
		SqlSession session = sqlSessionFactory.openSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		int i = userMapper.deleteUserByUid(17);
		System.out.println("删除了"+i+"条记录");
		session.commit();
		session.close();
	}
	
	@Test
	public void testUpdateUser() throws Exception {
		SqlSession session = sqlSessionFactory.openSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		User user = userMapper.findUserByUid(7);
		user.setuName("你好");
		user.setuPwd("1234");
		user.setuPhone("23156587691");
		int i = userMapper.updateUser(user);
		System.out.println("修改了"+i+"条记录");
		session.commit();
		session.close();
	}
	
	@Test
	public void testaddUser() throws Exception {
		SqlSession session = sqlSessionFactory.openSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		User user = new User("ttd","1234","13825436380");
		int i = userMapper.addUser(user);
		System.out.println("添加了"+i+"条记录");
		session.commit();
		session.close();
	}
	
	@Test
	public void testfindUsersByConditions() throws Exception {
		SqlSession session = sqlSessionFactory.openSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		User user = new User();
		user.setuId(1);
		List users = userMapper.findUsersByConditions(user);
		for(User u : users) {
			System.out.println(u.toString());
		}
		session.close();
	}
	
	@Test
	public void testfindUserMap() throws Exception {
		SqlSession session = sqlSessionFactory.openSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		User user = new User();
		user.setuId(1);
		List> users = userMapper.findUserMap(user);
		for(Map m : users) {
			System.out.println(m.get("uId"));
			System.out.println(m.get("uName"));//键的名字对应实体类的属性名
		}
		session.close();
	}
	
	@Test
	public void foreachTest1() throws Exception {
		SqlSession session = sqlSessionFactory.openSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		Object[] uIds = {1,2,6,7};
		List users = userMapper.foreachTest1(uIds);
		for(User u : users) {
			System.out.println(u.toString());
		}
		session.close();
	}
	
	@Test
	public void foreachTest2() throws Exception {
		SqlSession session = sqlSessionFactory.openSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		List uIds = new ArrayList<>();
		uIds.add(1);
		uIds.add(2);
		uIds.add(7);
		List users = userMapper.foreachTest2(uIds);
		for(User u : users) {
			System.out.println(u.toString());
		}
		session.close();
	}
	
	@Test
	public void testupdateUser1() throws Exception {
		SqlSession session = sqlSessionFactory.openSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		User user = new User();
		user.setuId(2);
		user.setuName("cpx");
		int i = userMapper.updateUser1(user);
		System.out.println("修改了"+i+"条记录");
		session.commit();
		session.close();
	}
	
}

该测试类中首先写了init方法用作初始化,加载了sqlMapConfig.xml文件,我们对里面的方法进行解释:

①通过uid查询用户信息,这里实际上在xml文件中我们写了两种SQL语句,一种是直接写,另一种是把查询内容封装一下,写在sql标签内,然后再调用

②修改用户信息,我们在xml文件中也是写了两种方法,一种是直接写出需要修改的内容,另一种是if标签语句进行判断,把可能修改的参数都放进去

③通过条件进行查询用户信息,方法是findUsersByConditions,里面我们整合了where标签,它会判断第一个语句是否有and,如果有会自动删除

④遍历查询我们写了两种方式用于对比,一种是数组,一种是列表,里面都是使用foreach标签,差别其实不算大,传入的参数是用户编号,一个是Object数组,一个是List列表。

好了,本期博客就到这里了,下期我们将继续整理一对一,一对多以及多对多Mybatis语句的写法,下期再见!

 

你可能感兴趣的:(Java,Web)