码迷,mamicode.com
首页 > 数据库 > 详细

MyBatis 入门(二)--用接口方式访问数据库

时间:2016-05-12 18:40:48      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:

一、建立接口 UserMapper.java

public interface UserMapper {
    public List<User> getAllUser();
    public User getUserById(String userId);
    public int insert(User user);
    public int update(User user);
    public int delete(User user);
}

二 修改 UserMapper.xml

<?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.elements.user.mapper.UserMapper"> 
注意这里与原来的方式不同 这里必须和你的接口路径+名称一直(不包括.java)
  <resultMap type="com.elements.user.model.User" id="usermap">
        <id column="userId" property="userId"/>
        <result column="UserName" property="UserName"/>
        <result column="UserEmail" property="UserEmail"/>
    </resultMap>


    <select id="getAllUser" resultMap="usermap">
        select * from mybatis.user
    </select>

    <select id="getUserById" resultMap="usermap" parameterType="String">
        select * from mybatis.user where userId= #{userId}
    </select>


    <insert id="insert" parameterType="com.elements.user.model.User">
     insert into mybatis.user (UserName, UserEmail) values (
          #{UserName}, #{UserEmail}
     )
    </insert>

    <update id="update" parameterType="com.elements.user.model.User">
     update mybatis.user set UserName=#{UserName},
     UserEmail=#{UserEmail}
     where userId= #{userId}
    </update>

    <delete id="delete" parameterType="String">
     delete mybatis.user where userId= #{userId}
    </delete>

</mapper>

三、 mybatis-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>
    <properties resource="jdbc.properties" />
    <environments default="dev">
        <environment id="dev">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${driver}" />
                <property name="url" value="${url}" />
                <property name="username" value="${username}" />
                <property name="password" value="${password}" />
            </dataSource>
        </environment>
    </environments>
    <mappers>

        <mapper class="com.elements.user.mapper.UserMapper"/>
    </mappers>
</configuration>

四、测试类

public class TestUser {

    @Test
    public void TestSelectAll() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
                .build(inputStream);

        SqlSession session = sqlSessionFactory.openSession();

        try {

            UserMapper user = (UserMapper) session.getMapper(UserMapper.class);
            List<User> userlist = user.getAllUser();
            System.out.print(userlist);

        } finally {
            session.close();
        }

    }
}

项目代码下载:http://pan.baidu.com/s/1c12EDaC

五、不在mybatis-config.xml配置方式

public class TestUser {

    @Test
    public void TestSelectAll() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
                .build(inputStream);

        SqlSession session = sqlSessionFactory.openSession();

        if(!session.getConfiguration().getMapperRegistry().hasMapper(UserMapper.class))
            session.getConfiguration().addMapper(UserMapper.class);
        //这里获取session的配置文件,在MapperRegistry查看我们的接口方式是否已经注册
        //如果没有注册 则添加我们的接口

        try {

            UserMapper user = (UserMapper) session.getMapper(UserMapper.class);
            List<User> userlist = user.getAllUser();
            System.out.print(userlist);

        } finally {
            session.close();
        }

    }
}

项目代码下载:http://pan.baidu.com/s/1slBrQ0X

MyBatis 入门(二)--用接口方式访问数据库

标签:

原文地址:http://blog.csdn.net/likewindy/article/details/51350632

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