Mybatis随笔

MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。

话不多说,先上代码:


maven项目的目录结构:
Mybatis随笔_第1张图片

1、利用maven导包:

    
    <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatisartifactId>
        <version>3.2.8version>
    dependency>
    
    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>4.12version>
    dependency>
    <dependency>
        <groupId>javax.annotationgroupId>
        <artifactId>javax.annotation-apiartifactId>
        <version>1.2version>
    dependency>
    
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <version>3.1.12version>
    dependency>

2、新建一个实体类——Dept

        public class Dept implements Serializable {

            private Integer id;
            private String name;
            private String loc;

            public Integer getId() {
                return id;
            }
            public String getName() {
                return name;
            }
            public String getLoc() {
                return loc;
            }
            public void setId(Integer id) {
                this.id = id;
            }
            public void setName(String name) {
                this.name = name;
            }
            public void setLoc(String loc) {
                this.loc = loc;
            }
            @Override
            public String toString() {
                return "Dept [id=" + id + ", name=" + name + ", loc=" + loc + "]";
            }
        }

3、创建一个数据访问接口类(DAO)——DeptDAO

        public interface DeptDAO {
            //一个查询所有数据方法
            public ListfindAll();
        }

4、添加Mybatis映射文件(一般一个DAO对应一个映射文件)——DeptMapper.xml

          
        
        
        <mapper namespace="dao.DeptDAO">

            <resultMap type="entity.Dept" id="deptResultMap">
                <result property="name" column="deptName">result>
            resultMap>

            
            <select id="findAll" resultMap="deptResultMap">
                SELECT id,deptName,loc FROM dept
            select>

        mapper>

5、添加mybatis的配置文件——SqlMapConfig.xml

          
        
        <configuration>
            <environments default="environment">
                <environment id="environment">
                    <transactionManager type="JDBC" />
                    <dataSource type="POOLED">
                        <property name="driver" value="com.mysql.jdbc.Driver" />
                        <property name="url"
                            value="jdbc:mysql://localhost:3306/db_book?characterEncoding=UTF-8" />
                        <property name="username" value="root" />
                        <property name="password" value="xxxxxx" />
                    dataSource>
                environment>
            environments>
            
            <mappers>
                <mapper resource="entity/DeptMapper.xml" />
            mappers>
        configuration> 

6、一切就绪,接下来就是测试了,创建一个test测试类

        public class TestCase {

        @Test
        //不使用映射器
        public void test(){
            SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
            SqlSessionFactory ssf = ssfb.build(TestCase.class.getClassLoader().getResourceAsStream("SqlMapConfig.xml"));
            SqlSession session = ssf.openSession();
            //调用SqlSession查询方法出入mapper中的id
            Listlists = session.selectList("dao.DeptDAO.findAll");
            System.out.println(lists);
        }

        @Test
        //使用映射器
        public void test1(){
            SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
            SqlSessionFactory ssf = ssfb.build(TestCase.class.getClassLoader().getResourceAsStream("SqlMapConfig.xml"));
            SqlSession session = ssf.openSession();
            DeptDAO dao = session.getMapper(DeptDAO.class);
            Listlists = dao.findAll();
            System.out.println(lists);

        }
    }
        未完待续、、、、、

你可能感兴趣的:(mybatis)