使用mybatis的dao層的開發方式有兩種,一種是類似與ssh框架,定義介面,然後定義實現類,還有一種方式就是動態代理方式,即只需要定義介面和方法就可以動態地去執行sql語句。

  1、在com.iteason.mapper目錄下建立UserMapper介面,用於定義所有的行為

public interface UserMapper {

    //遵循四個原則

    //介面方法名即為User.xml中的id名

    //返回值型別和User.xml中的型別要一致

    //方法的引數型別要和User.xml中的型別一致

    //介面與相應的User.xml的名稱空間繫結


    //通過id查

    public User findUserById(Integer id);

    //通過使用者名稱模糊查

    public List<User> findByUsername(String username);

    //插入User

    public void insertUser(User user);

    //更新User

    public void updateUser(Integer id);

    //通過id刪除User

    public void deleteUser(Integer id);


}

   2、在com.iteason.mapper目錄下建立UserMapper.xml配置檔案,用於對映UserMapper介面和User類

<?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.iteason.mapper.UserMapper">




<!-- 通過id查詢 -->

<select id="findUserById" parameterType="Integer" resultType="User">

select * from user where id = #{id}

</select>



<!-- 通過uesrname模糊查詢 -->

<select id="findUserByUsername" parameterType="String" resultType="User">

select * from user where username like #{username}

</select>



<!-- 插入 -->

<insert id="insertUser" parameterType="User" >

<selectKey order="AFTER" keyProperty="id" resultType="Integer">

select LAST_INSERT_ID()

</selectKey>

insert into user(username,birthday,address,sex) values(#{username},#{birthday},#{address},#{sex});

</insert>


<!-- 更新 -->

<update id="updateUser" parameterType="Integer">

update user set username = "hahaha" where id = #{id}

</update>


<!-- 刪除 -->

<delete id="deleteUser" parameterType="Integer">

delete from user where id = #{id}

</delete>

</mapper>

 3、在SqlMapConfig.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>


<!-- 讀取jdbc配置檔案 -->

<properties resource="jdbc.properties"/>


<!-- 設定別名,包下的類名即為別名 -->

<typeAliases>

<package name="com.iteason.pojo"/>

</typeAliases>


<!-- 和spring整合後 environments配置將廢除 -->

<environments default="development">

<environment id="development">

<!-- 使用jdbc事務管理 -->

<transactionManager type="JDBC" />

<!-- 資料庫連線池 -->

<dataSource type="POOLED">

<property name="driver" value="${jdbc.driver}" />

<property name="url"

value="${jdbc.url}" />

<property name="username" value="${jdbc.username}" />

<property name="password" value="${jdbc.password}" />

</dataSource>

</environment>

</environments>


<!-- 包下批量掃描對映檔案,對映檔案前面的名字要和mapper介面一樣 -->

<mappers>

<package name="com.iteason.mapper"/>

</mappers>

</configuration>

4、測試增加User
​​​​​​

@Test

public void insertUser() throws Exception {

    //載入核心配置檔案

    String resource = "sqlMapConfig.xml";

    InputStream in = Resources.getResourceAsStream(resource);

    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);

    SqlSession sqlSession = factory.openSession();


    UserMapper mapper = sqlSession.getMapper(UserMapper.class);

    User user = new User();

    user.setId(1212);

    user.setUsername("hahahahaa");

    mapper.insertUser(user);

    sqlSession.commit();

}

5、測試刪除User
​​​​​​

@Test

public void deleteTest() throws Exception {

    //載入核心配置檔案

    String resource = "sqlMapConfig.xml";

    InputStream in = Resources.getResourceAsStream(resource);

    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);

    SqlSession sqlSession = factory.openSession();

    UserMapper mapper = sqlSession.getMapper(UserMapper.class);


    mapper.deleteUser(31);


    sqlSession.commit();


}

6、測試更新User
​​​​​​

@Test

public void testUpdate() throws Exception {

    //載入核心配置檔案

    String resource = "sqlMapConfig.xml";

    InputStream in = Resources.getResourceAsStream(resource);

    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);

    SqlSession sqlSession = factory.openSession();
    
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);


    mapper.updateUser(1);


    sqlSession.commit();


}

7、測試查詢User

@Test

public void selectUser() throws Exception {

    //載入核心配置檔案

    String resource = "sqlMapConfig.xml";

    InputStream in = Resources.getResourceAsStream(resource);

    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);

    SqlSession sqlSession = factory.openSession();

    UserMapper mapper = sqlSession.getMapper(UserMapper.class);

    User user = mapper.findUserById(10);

    System.out.println(user);

}