MyBatis更新,删除,插入

UserMapper.java:

package com.bjsxt.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.bjsxt.pojo.User;

public interface UserMapper {
	
	/**
	 * mysql动态查询语句
	 * @param id
	 * @param username
	 * @param password
	 * @return
	 */
	List selByIUP(@Param("id") int id,@Param("username") String username,@Param("password") String password);
	
	/**
	 *	通过用户去修改数据
	 * @param user
	 * @return
	 */
	int update(User user);
	
	/**
	 * 	使用in进行选择查询
	 * @param list
	 * @return
	 */
	List selin(@Param("list") List list);
	
	
	/**
	 * 	通过用户名进行表的模糊查询
	 * @param username
	 * @return
	 */
	List selLike(@Param("username") String username);
	
}

UserMapper:



  
  
  	
  
  	
  
 		 
  	
  		update t_user
  		
  			
  				username=#{username},
  			
  			
  				password=#{password},
  			
  		
  		where id=#{id}
  	
  	
  	
  	
  

User实体类:

package com.bjsxt.pojo;

import java.io.Serializable;

public class User implements Serializable{
	private int id;
	private String username;
	private String password;
	public int getId() {
		return id;
	}
	public void setId(int 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 + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		result = prime * result + ((password == null) ? 0 : password.hashCode());
		result = prime * result + ((username == null) ? 0 : username.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (id != other.id)
			return false;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		if (username == null) {
			if (other.username != null)
				return false;
		} else if (!username.equals(other.username))
			return false;
		return true;
	}
	public User() {
		super();
	}
	
}

工具类:

package com.bjsxt.util;

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;

public class MyBatisUtil {
	private static SqlSessionFactory factory=null;
	static{
		try {
			InputStream	is = Resources.getResourceAsStream("mybatis-cfg.xml");
			 factory = new SqlSessionFactoryBuilder().build(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	public static SqlSession getSession() {
		SqlSession session =null;
		if (factory!=null) {
			 session = factory.openSession(false);
		}
		return session;
	}
}

配置文件mybatis:



  
  
  
  
  	
  
  
  	
  
  	
 		
 			
 			
 				
 				
 				
 				
 				
 			
 		
  	
  	
  		
  	
  

 

 

 增删改查:



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

测试类:

package com.bjsxt.test;

import java.util.List;

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

import com.bjsxt.mapper.UserMapper;
import com.bjsxt.pojo.User;
import com.bjsxt.util.MyBatisUtil;

public class TestZSGC {
	
	@Test
	public void TestInsert() {
		SqlSession session = MyBatisUtil.getSession();
		User user=new User();
		user.setUsername("杨杰");
		user.setPassword("250");
		int num = session.insert("com.bjsxt.mapper.UserMapper.instr", user);
		if (num>0) {
			System.out.println("插入成功!!");
		}else {
			System.out.println("插入失败!!");
		}
		session.close();
	}
	
	@Test
	public void Testupdate() {
		SqlSession session = MyBatisUtil.getSession();
		
		User user=new User();
		user.setId(15);
		user.setUsername("杨杰250");
		user.setPassword("就是250");
		int updatenum = session.update("com.bjsxt.mapper.UserMapper.update", user);
		if (updatenum>0) {
			System.out.println("更新成功!!");
		}else {
			System.out.println("更新失败!!");
		}
		session.close();
	}
	
	@Test
	public void Testdelete() {
		SqlSession session = MyBatisUtil.getSession();
		int delete = session.delete("com.bjsxt.mapper.UserMapper.delete", 8);
		System.out.println(delete);
		session.close();
	}
	
	@Test
	public void TestSelectAll() {
		SqlSession session = MyBatisUtil.getSession();
		UserMapper mapper = session.getMapper(UserMapper.class);
		List list = mapper.selAll();
		for (User u : list) {
			System.out.println(u);
		}
		session.close();
	}
	
	@Test
	public void TestselByUP() {
		SqlSession session = MyBatisUtil.getSession();
		UserMapper mapper = session.getMapper(UserMapper.class);
		User u = mapper.selByUP("杨杰250", "就是250");
		System.out.println(u);
		session.close();
	}
	
}

UserMapper接口(增删改都是在测试类实现的,没用接口绑定!!!):

package com.bjsxt.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.bjsxt.pojo.User;

public interface UserMapper {
	
	/**
	 * 查询所有用户信息
	 * @return
	 */
	List selAll(); 
	
	/**
	 * 	通过username和password去查询用户信息
	 * @param username
	 * @param password
	 * @return
	 */
	User selByUP(@Param("username") String username,@Param("password") String password);
}

数据库还有log4j的配置文件和MyBatis动态查询一样。这里的测试全部都是Junit测试,没用main方法实现。

如果不想等值查询,也有非等值查询,只不过需要转移字符( <    是   &Lt   L需要小写的 ):

  1. 和           &      & amp;  
  2. 单 引号     '      & apos;  
  3. 双引 号     "      & quot;  
  4. 大于 号     >      & gt;  
  5. 小于 号     <      & lt;   

 

你可能感兴趣的:(数据库学习,MyBatis更新,删除,插入)