mybatis入门(实现增删改查)

目录

  1.查询数据库中id=1的记录

2.通过用户名模糊查询记录

3.插入数据

4.根据id修改用户名

5.根据id删除记录

6.使用mybatis开发dao层(查询id为1的记录。对比上面的)


新建javaProject 

导入数据库驱动包:

导入mybatis包和它的依赖包:

mybatis入门(实现增删改查)_第1张图片mybatis入门(实现增删改查)_第2张图片

配置约束:https://blog.csdn.net/qq_40323256/article/details/89705297

项目文件如下:

mybatis入门(实现增删改查)_第3张图片           数据库:mybatis入门(实现增删改查)_第4张图片

 

全局配置(SqlMapConfig.xml)



 

	

	
		
			
			
			
			
				
				
				
				
			
		
	
	
	
		


	
	
	

注意:SqlMapConfig.xml中还可以使用读取配置文件(db.properties)的方式连接数据库,如下:



 


	

	

	
		
			
			
			
			
				
				
				
				
			
		
	
	
	
		

	
	
	

db.properties:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/web01
jdbc.username=root
jdbc.password=root

  1.查询数据库中id=1的记录

UserMapper.xml:



 


	

User.java:

package com.sikiedu.bean;

public class User {
	private Integer id;
	private String username;
	private String password;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer 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;
	}
	
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
	}
	
	
}

HelloMyBatis.java:

package test;

import java.io.IOException;
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;
import org.junit.Test;

import com.sikiedu.bean.User;

public class HelloMyBatis {

	@Test
	public void Test() throws IOException{
		String resource="sqlMapConfig.xml";
		InputStream in = Resources.getResourceAsStream(resource);
		SqlSessionFactoryBuilder ssfb=new SqlSessionFactoryBuilder();
		SqlSessionFactory ssf=ssfb.build(in);
		SqlSession session  = ssf.openSession();
		
		User user = session.selectOne("UserMapper.selectUserById",1);
		System.out.println(user);
	}
}

 运行结果:

mybatis入门(实现增删改查)_第5张图片

2.通过用户名模糊查询记录

HelloBatis.java:

	@Test
	public void Test2() throws IOException{
		String resource="sqlMapConfig.xml";
		InputStream in = Resources.getResourceAsStream(resource);
		SqlSessionFactoryBuilder ssfb=new SqlSessionFactoryBuilder();
		SqlSessionFactory ssf=ssfb.build(in);
		SqlSession session  = ssf.openSession();
		
		List list = session.selectList("UserMapper.selectUserByName", "李");
		for (User u : list) {
			System.out.println(u);
		}
	}

UserMapper.xml:



	

运行结果:

mybatis入门(实现增删改查)_第6张图片

3.插入数据

HelloMyBatis.java:

	@Test
	public void Test3() throws IOException{
		String resource="sqlMapConfig.xml";
		InputStream in = Resources.getResourceAsStream(resource);
		SqlSessionFactoryBuilder ssfb=new SqlSessionFactoryBuilder();
		SqlSessionFactory ssf=ssfb.build(in);
		SqlSession session  = ssf.openSession();
		
		User user=new User();
		user.setUsername("小明");
		user.setPassword("1");
		
		session.insert("UserMapper.insertUser",user);
		session.commit();
	}

UserMapper.xml:

	
		insert into user values(null,#{username},#{password})
	

运行结果:

mybatis入门(实现增删改查)_第7张图片

4.根据id修改用户名

HelloMyBatis.java:

	@Test
	public void Test4() throws IOException{
		String resource="sqlMapConfig.xml";
		InputStream in = Resources.getResourceAsStream(resource);
		SqlSessionFactoryBuilder ssfb=new SqlSessionFactoryBuilder();
		SqlSessionFactory ssf=ssfb.build(in);
		SqlSession session  = ssf.openSession();
		
		User user=new User();
		user.setId(14);
		user.setUsername("小疆");
		
		session.insert("UserMapper.updateUser",user);
		session.commit();
	}

UserMapper.xml:

	
		update user set username=#{username} where id=#{id}
	

运行结果:

mybatis入门(实现增删改查)_第8张图片

5.根据id删除记录

HelloMyBatis.java:

	@Test
	public void Test5() throws IOException{
		String resource="sqlMapConfig.xml";
		InputStream in = Resources.getResourceAsStream(resource);
		SqlSessionFactoryBuilder ssfb=new SqlSessionFactoryBuilder();
		SqlSessionFactory ssf=ssfb.build(in);
		SqlSession session  = ssf.openSession();
		
		session.delete("UserMapper.deleteUser",14);
		session.commit();
	}

UserMapper.xml:

	
		delete from user where id=#{id}
	

运行结果:id=14的记录被删除了

mybatis入门(实现增删改查)_第9张图片

6.使用mybatis开发dao层(查询id为1的记录。对比上面的)

mybatis入门(实现增删改查)_第10张图片

UserDao.java:

package com.sikiedu.dao;

import com.sikiedu.bean.User;

public interface UserDao {

		User getUserById(Integer id);
	
}

UserDaolmpl.java:

package com.sikiedu.dao;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.sikiedu.bean.User;

public class UserDaolmpl implements UserDao {
	SqlSessionFactory ssf;

	
	public UserDaolmpl(SqlSessionFactory ssf) {
		super();
		this.ssf = ssf;
	}


	@Override
	public User getUserById(Integer id) {
		SqlSession session  = ssf.openSession();
		return session.selectOne("UserMapper.selectUserById",id);
	}

}

UserDaoTest.java:

package test;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.sikiedu.bean.User;
import com.sikiedu.dao.UserDao;
import com.sikiedu.dao.UserDaolmpl;

public class UserDaoTest {

	private static SqlSessionFactory ssf;
	
	static{
		String resource="sqlMapConfig.xml";
		InputStream in;
		try {
			in = Resources.getResourceAsStream(resource);
			SqlSessionFactoryBuilder ssfb=new SqlSessionFactoryBuilder();
			ssf=ssfb.build(in);
			in.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@Test
	public void DaoTest(){
		UserDao dao=new UserDaolmpl(ssf);
		User user=dao.getUserById(1);
		System.out.println(user);
	}
}

运行结果:

7.包装类

UserVo.java:

package com.sikiedu.bean;

public class UserVo {
	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
}

UserMapper.java:

	public User selectUserByUserVo(UserVo vo);

 UserMapper.xml: 

	

MapperTest.java:

	@Test
	public void Test3() throws IOException{
		String resource="sqlMapConfig.xml";
		InputStream in = Resources.getResourceAsStream(resource);
		SqlSessionFactoryBuilder ssfb=new SqlSessionFactoryBuilder();
		SqlSessionFactory ssf=ssfb.build(in);
		SqlSession session  = ssf.openSession();
		
		UserMapper mapper = session.getMapper(UserMapper.class);
		UserVo vo=new UserVo();
		User u=new User();
		u.setId(1);
		vo.setUser(u);
		
		User user = mapper.selectUserByUserVo(vo);
		System.out.println(user);
	}

运行结果:

你可能感兴趣的:(mybatis,mybatis)