mybatis入门

而你用mybatis向你的种蒙上加文件架包如图所示
MyBatis 1

每一个MyBatis应用还盖一个SqlSessionFactory对象的实例为中心,SqlSessionFactory对象好通过SqlSessionFactoryBuilder对象来得到,SqlSessionFactoryBuilder可以根据XML配置文件创建出SqlSessionFactory对象。

配置文件config.xml
    <?xml version="1.0" encoding="UTF-8" ?>  
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
        "http://mybatis.org/dtd/mybatis-3-config.dtd">  
    <configuration>  

        <typeAliases>  
            <typeAlias alias="User" type="com.lovo.entity.User" />  <指定实体类及对应的包路径>
            <typeAlias alias="Article" type="com.lovo.entity.Article" />  
        </typeAliases>  

        <environments default="development">  
            <environment id="development">  <配置数据库连接资源>
                <transactionManager type="JDBC" />  
                <dataSource type="POOLED">  
                    <property name="driver" value="com.mysql.jdbc.Driver" />  
                    <property name="url" value="jdbc:mysql://localhost:3306/mybatis" />  
                    <property name="username" value="root" />  
                    <property name="password" value="123456" />  
                </dataSource>  
            </environment>  
        </environments>  

        <mappers>  
            <mapper resource="com/lovo/entity/User.xml" />  <把实体类的xml文件资源注入到config.xml文件中>
            <mapper resource="com/lovo/entity/Article.xml" />  
        </mappers>  
    </configuration>  
可以通过SessionFactory对象的openSession()方法创建SqlSession对象,SqlSession 对象完全包含以数据库为背景的所有执行SQL操作的方法,可以用SqlSession实例来直接执行已映射的SQL语句;当然,更简洁的方式是使用接口来封装对数据的操作

User实体类

    package com.lovo.entity;  

    public class User {  
        private String username;  
        private String password;  
        private String email;  

        public String getUsername() {  
            return username;  
        }  

        public void setUsername(String username) {  
            this.username = username;  
        }  

        public String getPassword() {  
            return password;  
        }  

        public void setPassword(String password) {  
            this.password = password;  
        }  

        public String getEmail() {  
            return email;  
        }  

        public void setEmail(String email) {  
            this.email = email;  
        }  

        @Override  
        public String toString() {  
            return "User [username=" + username + ", password=" + password  
                    + ", email=" + email + "]";  
        }  

    }  

Article实体类

    package com.lovo.entity;  

    import java.util.Date;  

    public class Article {  
        private int id;  
        private String content;  
        private User user;  
        private Date pubdate;  

        public int getId() {  
            return id;  
        }  

        public void setId(int id) {  
            this.id = id;  
        }  

        public String getContent() {  
            return content;  
        }  

        public void setContent(String content) {  
            this.content = content;  
        }  

        public User getUser() {  
            return user;  
        }  

        public void setUser(User user) {  
            this.user = user;  
        }  

        public Date getPubdate() {  
            return pubdate;  
        }  

        public void setPubdate(Date pubdate) {  
            this.pubdate = pubdate;  
        }  

        @Override  
        public String toString() {  
            return "Article [id=" + id + ", content=" + content + ", user=" + user.toString()  
                    + ", pubdate=" + pubdate + "]";  
        }  


    }  

User映射文件user.xml 这文件被的id和接口中的计名字一样便可以

    <?xml version="1.0" encoding="UTF-8" ?>  
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  

    <mapper namespace="com.lovo.dao.UserDao">  
        <select id="findByUsername" parameterType="String" resultType="User">  
            select * from tb_user where username=#{username}  
        </select>  

        <select id="findAll" resultType="User">  
            select * from tb_user  
        </select>  

        <insert id="save" parameterType="User" keyProperty="username">  
            insert into tb_user (username, password, email) values (#{username}, #{password}, #{email})  
        </insert>  
    </mapper>  

Article映射文件

    <?xml version="1.0" encoding="UTF-8" ?>  
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  

    <mapper namespace="com.lovo.dao.ArticleDao">  
        <resultMap type="Article" id="resultArticle">  
            <id property="id" column="id"/>  
            <result property="content" column="content"/>  
            <result property="pubdate" column="pubdate"/>  
            <association property="user" javaType="User">  
                <id property="username" column="username" />  
                <result property="email" column="email"/>  
            </association>  
        </resultMap>  

        <select id="findAll" resultMap="resultArticle">  
            select t1.username, t1.email, t2.id, t2.content, t2.pubdate from tb_user as t1, tb_article as t2 where t1.username=t2.userid  
        </select>  
    </mapper>  

MyBatis用户接口

    package com.lovo.dao;  

    import java.util.List;  

    import com.lovo.entity.User;  

    public interface UserDao {  

        public User findByUsername(String username);  

        public List<User> findAll();  

        public void save(User user);  
    }  

Article类的接口

    package com.lovo.dao;  

    import java.util.List;  

    import com.lovo.entity.Article;  

    public interface ArticleDao {  

        public List<Article> findAll();  
    }  

测试代码,这些办法应该写以dao层的兑现类似吃(同时可以针对seesion工厂得到近似进行打包,封装成只要传一个实体类对象名字就是能够开拓seesion和)

    package com.lovo.test;  

    import java.io.Reader;  
    import java.util.List;  

    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.lovo.dao.ArticleDao;  
    import com.lovo.entity.Article;  

    public class Test {  

        public static void main(String[] args) throws Exception {  
            Reader reader = Resources.getResourceAsReader("config.xml");  
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);  
            SqlSession session = sessionFactory.openSession();  
            try {  
                ArticleDao aDao = session.getMapper(ArticleDao.class);  
                List<Article> list = aDao.findAll();  
                for(Article a: list) {  
                    System.out.println(a);  
                }  
                System.out.println("End!");  
            } finally {  
                session.close();  
            }  

        }  
    }  

用指出的凡SqlSessionFactoryBuilder类的目标要创立有SqlSessionFactory后哪怕从不就此了,可以为丢掉;而SqlSessionFactory对象要让创造,应该当尽应用程序执行中都是有的,最好是做成被别程序共享的单例。对于SqlSession,每个线程都该生出其自己的SqlSession实例。SqlSession的实例不能够为共享,因为它是线程不安全之,它的顶尖作用范围是告或艺术范围。绝对免克将SqlSession实例的援位于一个近似的静态字段甚至是实例字段受到,也休想会SqlSession实例的援在另项目的田间管理范围受到,例如Serlvet的HttpSession中。不管您利用什么的Web框架,要考虑将SqlSession放在一个和
HTTP 请求对象一般之打算范围外。换句话说,基于收到的 HTTP
请求,你可打开 了一个
SqlSession,然后回响应,就得关闭它了。关闭Session很重点,最好的选料是于finally代码块被关闭它。

网站地图xml地图