当前位置 : 主页 > 编程语言 > c++ >

mybaits配置及其对数据库数据的增删改查

来源:互联网 收集:自由互联 发布时间:2021-07-03
test1 public static void main(String[] args) throws Exception{String resource="mybatis.xml";InputStream inputStream=Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream
test1
public static void main(String[] args) throws Exception{
		String resource="mybatis.xml";
		InputStream inputStream=Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session=sqlSessionFactory.openSession();
		System.out.println(session);
		
		UserInfo info=session.selectOne("user.getUsersById",101);
		info.setPwd("a");
		session.update("user.update",info);
		System.out.println(info);	
		UserInfo info2=new UserInfo(105,"abc","123",2);
		session.insert("user.add",info2);
		session.update("user.update",info);
		
		session.delete("user.deleteById",105);

//		Map
 
   map=new HashMap
  
   (); // map.put("id", 104); // map.put("name", "aaa"); // map.put("pwd", "123"); // map.put("rolesid", 2); //// session.insert("user.add",map); // System.out.print(session.insert("user.selectBySql",map)); System.out.println(session.selectOne("user.getUsersById",104)); // Map
   
     map=new HashMap
    
     (); map.put("sql", "select * from userinfo"); List
     
       list=session.selectList("user.selectBySql", map); for (UserInfo userInfo : list) { System.out.println(userInfo); } session.commit(); session.close(); } }
     
    
   
  
 
mapper.xml
 

  
 
  	
  
  	
  
  	
	
  
    insert into userinfo values(#{id},#{name},#{pwd},#{rolesid}) 
  
  	
  	
  
    delete userinfo where id=#{id} 
  
  	
  	
  
    update userinfo set name=#{name},pwd=#{pwd},rolesid=#{rolesid} where id=#{id} 
  
  	
  	
  	
  
  
 
增加dao层,编写dao层接口,利用mapper实现
package dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import bean.UserInfo;

public interface UserInfoDao {
	List
 
    getUserAll();
	List
  
    getUserById(int id); int add(UserInfo info); int deleteById(int id); List
   
     getUserByNameAndPwd(@Param("name") String name,@Param("pwd") String pwd); }
   
  
 
日志(方便调试和纠错)
log4j.rootLogger=error,Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
test2
public static void main(String[] args) {
		
		SqlSession session1=getSqlSession();
		UserInfoDao dao1=session1.getMapper(UserInfoDao.class);
		System.out.println(dao1.getUserById(101));
		
//		List
 
   list=session1.getMapper(UserInfoDao.class).getUserAll();
		List
  
    list=dao1.getUserAll(); System.out.println(list); UserInfo info = new UserInfo(105,"abd","123",2); System.out.println(dao1.add(info)); Map
   
     map=new HashMap
    
     (); map.put("id", 105); map.put("name", "ooo"); map.put("pwd", "123"); map.put("rolesid", 3); System.out.println(session1.insert("dao.UserInfoDao.add", map)); dao1.deleteById(105); // session1.commit(); session1.close(); } static{ try { String resource="mybatis.xml"; InputStream inputstream =Resources.getResourceAsStream(resource); sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputstream); } catch (Exception e) { e.printStackTrace(); } } //SqlSession不可以使用单例,SqlSessionFactory可以使用 public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
    
   
  
 
动态添加sql语句进行多表联查
 
  		
  
  		
  
  		
  
  		
  
  		
   
    
    
  
  	
 
  	
 
  
  		u.*,r.id as "rinfo.id",r.name as "rinfo.name"
  	
 
    
    
 
  
  		update userinfo
   
   
     name=#{name}, 
    
   
     pwd=#{pwd}, 
    
   
     rolesid=#{rolesid}, 
    
  
  		  			where id=102
  	
 
    
 
    dao.update(new UserInfo(0, "a", "b", null,null));
    SqlSession session=getSqlSession();
	UserInfoDao dao=session.getMapper(UserInfoDao.class);
网友评论