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

用mybaits对数据库进行增删改查

来源:互联网 收集:自由互联 发布时间:2021-07-03
test.java public static void main(String[] args) throws Exception{String resource="mybatis.xml";InputStream inputStream=Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputSt
test.java
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} 
  
  	
  	
  	
  
  
 
网友评论