码迷,mamicode.com
首页 > 其他好文 > 详细

myBatis实例

时间:2015-04-28 13:43:22      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

一.搭建环境,

建立数据库

1 CREATE TABLE user(
2        id int(11) not NULL AUTO_INCREMENT,
3        userName varchar(50) DEFAULT NULL,
4        userAge int(11) DEFAULT NULL,
5        userAddress varchar(200) DEFAULT NULL,
6        PRIMARY KEY(id)
7 )ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

建立一个myBatis的java项目(不需要web项目)

然后倒入jar包

技术分享

建总配置文件:

在src下建configration包.在建Configurations.xml文件

代码如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE configuration PUBLIC " -//mybatis.org//DTD Config 3.0//EN"
 3 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 4 <configuration>
 5     <typeAliases>
 6         <typeAlias type="com.user.User" alias="User"/>
 7     </typeAliases>
 8     <environments default="development">
 9         <environment id="development">
10             <transactionManager type="JDBC" />
11             <dataSource type="POOLED">
12                 <property name="driver" value="com.mysql.jdbc.Driver"/>
13                 <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis"/>
14                 <property name="username" value="root"/>
15                 <property name="password" value="123"/>
16             </dataSource>
17         </environment>
18     </environments>
19     
20     <mappers>
21         <mapper resource="configration/User.xml"/>

    </mappers> 24 </configuration>

建user的实体类和映射文件:

com.user.User代码:

 1 package com.user;
 2 
 3 public class User {
 4     private int id;
 5     private String userName;
 6     private String userAge;
 7     private String userAddress;
 8     public int getId() {
 9         return id;
10     }
11     public void setId(int id) {
12         this.id = id;
13     }
14     public String getUserName() {
15         return userName;
16     }
17     public void setUserName(String userName) {
18         this.userName = userName;
19     }
20     public String getUserAge() {
21         return userAge;
22     }
23     public void setUserAge(String userAge) {
24         this.userAge = userAge;
25     }
26     public String getUserAddress() {
27         return userAddress;
28     }
29     public void setUserAddress(String userAddress) {
30         this.userAddress = userAddress;
31     }
32     
33 }

configration包下见User.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 4 
 5 <mapper namespace="User">
 6     <select id="selectUserById" parameterType="int" resultType="User">
 7         select * from user where id=#{id}
 8     </select>
 9     <select id="selectUserList" parameterType="int" resultType="User">
10         select * from user 
11     </select>
12 </mapper>

建测试类

 1 package test;
 2 
 3 import java.io.Reader;
 4 import java.util.List;
 5 
 6 import org.apache.ibatis.io.Resources;
 7 import org.apache.ibatis.session.SqlSession;
 8 import org.apache.ibatis.session.SqlSessionFactory;
 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
10 
11 import com.user.User;
12 
13 public class Test {
14     private static SqlSessionFactory sqlSessionFactory;
15     private static Reader reader;
16 
17     static {
18         try {
19             reader = Resources.getResourceAsReader("configration/Configurations.xml");
20             sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
21         } catch (Exception e) {
22             e.printStackTrace();
23         }
24     }
25     public static void main(String[] args) {
26         SqlSession session = sqlSessionFactory.openSession();
27         try {
28             //User user = (User) session.selectOne("User.selectUserById", 2);
29             List<User> list=session.selectList("User.selectUserList");
30             for(User user:list){
31                 System.out.println("姓名:"+user.getUserName());
32                 System.out.println("年龄"+user.getUserAge());
33                 System.out.println("地址:"+user.getUserAddress());
34                 System.out.println("-----------------------------------------------------------");
35             }
36             
37         } catch (Exception e) {
38             System.out.println("出现异常了.....");
39             e.printStackTrace();
40         } finally {
41             session.close();
42         }
43     }
44 }

当你查看到从数据库中读取的数据时,说明环境搭建完毕了

二.用接口的方式实现编程.

建立IUserOperation.java

 1 package inter;
 2 
 3 import java.util.List;
 4 
 5 import com.user.User;
 6 
 7 public interface IUserOperation {
 8     public User selectUserById(int id);
 9     public List<User> selectUserList();
10 }

建立IUser.xml.

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 4 
 5 <mapper namespace="inter.IUserOperation">
 6     <select id="selectUserById" parameterType="int" resultType="User">
 7         select * from user where id=#{id}
 8     </select>
 9     <select id="selectUserList" resultType="User">
10         select * from user 
11     </select>
12 </mapper>

注意这个地方的namspace就不可以随意取名字了.必须是实现接口的地址.

在总配置文件中mappers添加一条xml:<mapper resource="configration/IUser.xml"/>

添加测试类:

 1 package test;
 2 
 3 import inter.IUserOperation;
 4 
 5 import java.io.Reader;
 6 import java.util.List;
 7 
 8 import org.apache.ibatis.io.Resources;
 9 import org.apache.ibatis.session.SqlSession;
10 import org.apache.ibatis.session.SqlSessionFactory;
11 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
12 
13 import com.user.User;
14 
15 /**
16  * 以接口的方式编程
17  * 如果以这种方式的话,那么对应的sql的xml配置文件中的namespace必须是实现接口的具体地址
18  */
19 public class TestInterface {
20     private static SqlSessionFactory sqlSessionFactory;
21     private static Reader reader;
22 
23     static {
24         try {
25             reader = Resources.getResourceAsReader("configration/Configurations.xml");
26             sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
27         } catch (Exception e) {
28             e.printStackTrace();
29         }
30     }
31     public static void main(String[] args) {
32         SqlSession session = sqlSessionFactory.openSession();
33         try {
34             IUserOperation userOperation=session.getMapper(IUserOperation.class);
35             /*User user=userOperation.selectUserById(3);
36             System.out.println(user.getUserAddress());
37             System.out.println(user.getUserName());*/
38             
39             List<User> list=userOperation.selectUserList();
40             for(User user:list){
41                 System.out.println("姓名:"+user.getUserName());
42                 System.out.println("年龄:"+user.getUserAge());
43                 System.out.println("地址:"+user.getUserAddress());
44                 System.out.println("--------------------------------");
45             }
46             
47             
48         } catch (Exception e) {
49             System.out.println("出现异常了.....");
50             e.printStackTrace();
51         } finally {
52             session.close();
53         }
54     }
55 }

 

myBatis实例

标签:

原文地址:http://www.cnblogs.com/guoyansi19900907/p/4462449.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!