初学mybatis--存个代码

目录结构

初学mybatis--存个代码_第1张图片

mybatis.xml




	
	
		
		
	
	
	
		
			
			
			
			
				
				
				
				
				
				
				
				
			
		
	
	
	
		
	

UserDao.xml





	
	
	
	
	
	
	
	
		insert into user values(#{id}, #{username}, #{password});
	
	
	 
	 	update user set username = #{username}, password = #{password} where id = #{id};
	 
	 
	 
	 	delete from user where id = #{0};
	 
	 
	 

User.java

package entity;

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

UserDao.java

package dao;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.Null;
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 com.mysql.cj.Session;

import entity.User;

public class UserDao {
	private static SqlSession session = null;
	
	@Before
	public void init() {
		try {
			// 1、需要执行sql语句(SqlSession--->SqlSessionFactory--->SqlSessionFactoryBuilder)
			SqlSessionFactoryBuilder sfb = new SqlSessionFactoryBuilder();
			// 将mybatis.xml主配置文件变成字节输入流
			InputStream is = Resources.getResourceAsStream("mybatis.xml");
			SqlSessionFactory ssf = sfb.build(is);
			session = ssf.openSession();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// 不带条件的查询
	@Test
	public void selectAll() throws IOException {
		/*
		 *  2、调用SqlSession类中的方法来执行sql语句
		 *  调用方式:namespace + "." + id
		 */
		List list = session.selectList("dao.UserDao.selectAll");
		// 打印结果
		for(User user : list) {
			System.out.println(user);
		}
	}
	
	// 通过id查询
	@Test
	public void selectById() {
		User user = session.selectOne("dao.UserDao.selectById", 2);
		System.out.println(user);
	}
	
	// 多个条件查询
	@Test
	public void selectByConditions() {
		Map paramter = new HashMap();
		paramter.put("id", 1);
		paramter.put("password", "aaa");
		List list = session.selectList("dao.UserDao.selectByConditions", paramter);
		System.out.println(list);
	}
	
	// 添加数据
	@Test
	public void addUser() {
		Map paramter = new HashMap();
		paramter.put("id", 3);
		paramter.put("username", "王五");
		paramter.put("password", "ccc");
		int count = session.insert("dao.UserDao.addUser", paramter);
		// 需要提交事务
		session.commit();
		if(count >= 1) {
			System.out.println("数据添加成功");
		} else {
			System.out.println("数据添加失败");
		}
	}
	
	// 修改数据
	@Test
	public void updateUser() {
		Map paramter = new HashMap();
		paramter.put("id", 2);
		paramter.put("username", "jack");
		paramter.put("password", "ddd");
		int count = session.update("dao.UserDao.updateUser", paramter);
		// 提交事务
		session.commit();
		System.out.println(count);
	}
	
	// 删除数据
	@Test
	public void deleteUser() {
		int count = session.delete("dao.UserDao.deleteUser", 3);
		// 提交事务
		session.commit();
		System.out.println(count);
	}
	
	// 动态查询
	@Test
	public void dynamicSelect() {
		Map paramter = new HashMap();
		paramter.put("id", 2);
		paramter.put("username", "jack");
		paramter.put("password", "ddd");
		List list = session.selectList("dao.UserDao.dynamicSelect", paramter);
		for(User user : list) {
			System.out.println(user);
		}
	}
}

 

你可能感兴趣的:(Mybatis)