mybatis实验(一)——对数据库进行简单的增、删、改、查

  1. 新建动态web工程,在src下创建Configuration.xml
  2. 复制mybatis框架所需的架包,到WEB-INF\lib目录下
  3. 配置Configuration.xml,设置mybatis的数据源的基础配置。初始配置代码如下:
    
    
    
       
         
             
         
        
            
            
                
                   
                   
                   
                   
                
            
        
         
             
              
             
    
    

 4.在src目录下创建“zz.mapping”包,该包用于保存javabean类的映射文件,在该保中创建“userMapping.xml”,文件初始配置代码如下:





     
     
     
 

 

 

5.创建数据库(mybatis),并创建user数据库表,并添加几条数据。该表结构如下:

mybatis实验(一)——对数据库进行简单的增、删、改、查_第1张图片

mybatis实验(一)——对数据库进行简单的增、删、改、查_第2张图片

 

6.在src目录下创建“zz.domian”包,该包用于保存user数据库的表的实体。在该包中创建User类,该类的属性对应user数据库表的字段。代码如下:

7.	package swu.zz.domian;
8.	
9.	public class User {
10.		private int id;
11.		private String userName;
12.		private int userAge;
13.		private String userAddress;
14.		
15.		public int getId() {
16.			return id;
17.		}
18.		public void setId(int id) {
19.			this.id = id;
20.		}
21.		public String getUserName() {
22.			return userName;
23.		}
24.		public void setUserName(String userName) {
25.			this.userName = userName;
26.		}
27.		public int getUserAge() {
28.			return userAge;
29.		}
30.		public void setUserAge(int userAge) {
31.			this.userAge = userAge;
32.		}
33.		public String getUserAddress() {
34.			return userAddress;
35.		}
36.		public void setUserAddress(String userAddress) {
37.			this.userAddress = userAddress;
38.		}
39.		@Override
40.		public String toString() {
41.			return "User [id=" + id + ", userName=" + userName + ", userAge="
42.					+ userAge + ", userAddress=" + userAddress + "]";
43.		}
44.		public User() {
45.			super();
46.		}
47.		
48.		
49.	
50.	}

 

7.在userMapping中定义4个方法,分别对应数据的增删改查。其代码如下:





 
	
	      
	

	
insert into user(userName,userAge,userAddress) values(#{userName},#{userAge},#{userAddress});
	



		delete from user where id=#{id};



	
		Update user
		SET 
		username = #{userName},
		userage = #{userAge},
		useraddress = #{userAddress}
		WHERE id = #{id}
	


	
	      
          
 

 

 

8.在Configuration.xml,下注册映射文件,定义别名,增加代码如下:




   
     
         
     
    
        
        
            
               
               
               
               
            
        
    
    
    
     
         
          
         

 9.在src下创建“zz.test”包,在包内添加“Test”类,用来测试、运行xml文件,代码如下:

package swu.zz.test;

import java.io.InputStream;
import java.util.List;

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

import swu.zz.domian.User;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String rs = "Configuration.xml";
		//加载配置文件
		InputStream is = Test.class.getClassLoader().getResourceAsStream(rs);
		
		//构建mybatis的工厂
		SqlSessionFactory sessionfactory = new SqlSessionFactoryBuilder().build(is);
		SqlSession session = sessionfactory.openSession();
		
		/*输出数据库中的数据*/
		String str1 = "swu.zz.mapping.userMapping.getAllUser";
		List myuser=session.selectList(str1);
		for(User user:myuser){
			System.out.println(user);
		}
	
/*向数据库增加数据*/
		String str3 = "swu.zz.mapping.userMapping.addUser";
		User user3 =new User();
		user3.setUserName("wang");
		int s = session.insert(str3, user3);
		session.commit()

		/*删除数据库中的数据*/
		String str4 = "swu.zz.mapping.userMapping.deleUser";
		int count = session.delete(str4, 2);
		session.commit();


/*修改数据*/
		String str5 = "swu.zz.mapping.userMapping.updateUser";
		User user5 = new User();
		user5.setId(5);
		user5.setUserName("kk");
		user5.setUserAge(23);
		user5.setUserAddress("北京");
		int c = session.update(str5, user5);
		session.commit();

		/*按id号来查找数据*/
		String str2 = "swu.zz.mapping.userMapping.getUser";
		User user1=session.selectOne(str2, 1);
		System.out.println(user1);

	}

}

 

你可能感兴趣的:(Java实验)