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

MyBatis初接触

时间:2014-05-08 22:40:09      阅读:415      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   tar   

参考MyBatis官方文档

基本开发步骤:

一、导包,mybatis-3.2.7.jar;

二、编写实体类Customer.java(与数据库表对应);

三、编写实体类对应的mapper接口CustomerMapper.java(定义实体类的操作);

四、编写实体类对应的mapperCustomer.xml(接口中的方法名与mapper中的操作的id名要一致);

五、编写mybatis-config.xml;

六、在mybatis-config.xml中注册mapper;

七、从xml获取SqlSessionFactory(或从mybatis提供的configuration class获取,具体参考mybatis-3.2.7手册,此手册位于mybatis的下载包中);

八、获取SqlSession;

九、通过SqlSession获取mapper,执行其中的操作。

以下是一个Demo:

1、Customer.java

bubuko.com,布布扣
 1 package com.mybatis.po;
 2 
 3 public class Customer {
 4     private int id;
 5     private String name;
 6     private String gender;
 7     private int age;
 8     private String address;
 9 
10     public int getId() {
11         return id;
12     }
13 
14     public void setId(int id) {
15         this.id = id;
16     }
17 
18     public String getName() {
19         return name;
20     }
21 
22     public void setName(String name) {
23         this.name = name;
24     }
25 
26     public String getGender() {
27         return gender;
28     }
29 
30     public void setGender(String gender) {
31         this.gender = gender;
32     }
33 
34     public int getAge() {
35         return age;
36     }
37 
38     public void setAge(int age) {
39         this.age = age;
40     }
41 
42     public String getAddress() {
43         return address;
44     }
45 
46     public void setAddress(String address) {
47         this.address = address;
48     }
49 
50 }
bubuko.com,布布扣

2、CustomerMapper.java

bubuko.com,布布扣
1 package com.mybatis.mapper;
2 
3 import com.mybatis.po.Customer;
4 
5 public interface CustomerMapper {
6     public int insert(Customer cus);
7     public int update(Customer cus);
8 }
bubuko.com,布布扣

3、Customer.xml

bubuko.com,布布扣
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper
 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 
 6 <mapper namespace="com.mybatis.mapper.CustomerMapper">
 7     <insert id="insert" parameterType="Customer">
 8         insert into customer(name, gender, age, address)
 9         values(#{name}, #{gender}, #{age}, #{address})
10     </insert>
11     
12     <update id="update" parameterType="Customer">
13         update customer
14         set name = #{name}, gender = #{gender}, age = #{age}, address = #{address}
15     </update>
16 </mapper>
bubuko.com,布布扣

4、mybatis-config.xml

bubuko.com,布布扣
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE configuration
 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 
 6 <configuration>
 7     <properties resource="db-config.properties" />
 8 
 9     <typeAliases>
10         <!-- <typeAlias type="com.mybatis.po.Customer" alias="Customer" /> -->
11         <package name="com.mybatis.po" />
12     </typeAliases>
13 
14     <environments default="development">
15         <environment id="development">
16             <transactionManager type="JDBC" />
17             <dataSource type="POOLED">
18                 <property name="driver" value="${driver}" />
19                 <property name="url" value="${url}" />
20                 <property name="username" value="${username}" />
21                 <property name="password" value="${password}" />
22             </dataSource>
23         </environment>
24     </environments>
25     
26     <mappers>
27         <mapper resource="com/mybatis/po/Customer.xml" />
28     </mappers>
29 </configuration>
bubuko.com,布布扣

5、db-config.properties

1 driver=com.mysql.jdbc.Driver
2 url=jdbc:mysql://127.0.0.1:3306/test
3 username=root
4 password=sa1234

6、获得SqlSessionFactory的工具类SessionFactoryUtil.java

bubuko.com,布布扣
 1 package com.mybatis.util;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 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 public class SessionFactoryUtil {
12     public SqlSessionFactory getFactory() throws Exception {
13         SqlSessionFactory factory = null;
14         try {
15             InputStream is = Resources
16                     .getResourceAsStream("mybatis-config.xml");
17             factory = new SqlSessionFactoryBuilder().build(is);
18         } catch (Exception e) {
19             throw e;
20         }
21         return factory;
22     }
23 
24     public void close(SqlSession session) {
25         if (session != null) {
26             session.close();
27         }
28     }
29 }
bubuko.com,布布扣

7、测试类TestCustomer.java

bubuko.com,布布扣
package test;

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;

import com.mybatis.mapper.CustomerMapper;
import com.mybatis.po.Customer;
import com.mybatis.util.SessionFactoryUtil;

public class TestCustomer {

    /**
     * @param args
     */
    public static void main(String[] args) {
        SqlSession session = null;
        SessionFactoryUtil util = null;
        try {
            util = new SessionFactoryUtil();
            session = util.getFactory().openSession();
            
            Customer cus = new Customer();
            cus.setName("Tom");
            cus.setGender("M");
            cus.setAge(20);
            cus.setAddress("USA");

            session.getMapper(CustomerMapper.class).insert(cus);
            session.commit();
        } catch (Exception e) {
            session.rollback();
            e.printStackTrace();
        } finally {
            util.close(session);
        }

        try {
            util = new SessionFactoryUtil();
            session = util.getFactory().openSession();
            
            Customer cus = new Customer();
            cus.setName("Tom");
            cus.setGender("M");
            cus.setAge(22);
            cus.setAddress("USA");

            session.getMapper(CustomerMapper.class).update(cus);
            session.commit();
        } catch (Exception e) {
            session.rollback();
            e.printStackTrace();
        } finally {
            util.close(session);
        }
    }

}
bubuko.com,布布扣

 8、结构

bubuko.com,布布扣

MyBatis初接触,布布扣,bubuko.com

MyBatis初接触

标签:style   blog   class   code   java   tar   

原文地址:http://www.cnblogs.com/nnxiaod/p/3716465.html

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