Integration of CRUD3 mybatis

Package Penalty for com.itheima.dao; 

Import com.itheima.domain.QueryVo;
 Import com.itheima.domain.User; 

Import java.util.List; 

/ ** 
 * @author dark horse programmer 
 * @Company HTTP: // the WWW. ithiema.com 
 * 
 persistence layer user Interface * 
 * / 
public  interface IUserDao { 

    / ** 
     * query for all users 
     * @return 
     * / 
    List <the user> the findAll (); 


    / ** 
     * the user information query id 
     * @param the userId 
     * @return 
     * / 
    the User the findById (Integer the userId);

    / ** 
     * The name of the user information fuzzy search 
     * @param username 
     * @return 
     * / 
    List <the User> the findByName (String username); 


    / ** 
     * The user query conditions in queryVo 
     * @param VO 
     * @return 
     * / 
    List <the user> findUserByVo (QueryVo VO); 

    / ** 
     * based on the passed parameter condition 
     * @param conditions user query: there may be a user name, it is possible to have sex, there may be an address, there may have 
     * @ return 
     * / 
    List <the user> findUserByCondition (the user user); 

    / ** 
     * id queryvo provided in accordance with a set of queries user information 
     *@param vo
     * @return
     */
    List<User> findUserInIds(QueryVo vo);
}
<? xml Version = "1.0" encoding = "UTF-8"?> 
<! DOCTYPE Mapper 
        the PUBLIC "- // mybatis.org//DTD Mapper 3.0 // EN" 
        "http://mybatis.org/dtd/mybatis mapper.dtd--3 "> 
<Mapper namespace =" com.itheima.dao.IUserDao "> 

    <-! correspondence between the configuration query result column names and attribute names entity class -> 
    <the resultMap ID =" s userMap " = type "USER"> 
        <-! corresponding primary key field -> 
        <Property ID = "the userId" column = "ID"> </ ID> 
        <-! corresponding to non-primary key field -> 
        <= Result Property "the userName" column = "username"> </ Result> 
        <Property Result = "userAddress" column = "address"></ the Result> 
        <Property the Result = "userSex" column = "Sex"> </ the Result> 
    <- content to know:! Repeat extraction sql statement ->
        <result property="userBirthday" column="birthday"></result>
    </resultMap>

    <sql id="defaultUser">
        select * from user
    </sql>

    <!-- 查询所有 -->
    <select id="findAll" resultMap="userMap">
        <include refid="defaultUser"></include>
    </select>

    <!-- 根据id查询用户 -->
    <select id="findById" parameterType="INT" resultMap="userMap">
        select * from user where id = #{uid}
    </select>

    <!-- 根据名称模糊查询 -->
    <select id="findByName" parameterType="string" resultMap="userMap">
    <-! querying user according to the condition of queryVo -></ SELECT>from WHERE username User name like # {}
   *
          SELECT

    <select id="findUserByVo" parameterType="com.itheima.domain.QueryVo" resultMap="userMap">
        select * from user where username like #{user.userName}
    </select>

    <!-- 根据条件查询
    <select id="findUserByCondition" resultMap="userMap" parameterType="user">
        select * from user where 1=1
        <if test="userName != null">
          and username = #{userName}
        </if>
        <if test="userSex != null">
            and sex = #{userSex}
        </if>
    </select>-->

    <select id="findUserByCondition" resultMap="userMap" parameterType="user">
        select * from user
        <where>
            <if test="userName != null">
                and username = #{userName}
            </if>
            <if test="userSex != null">
                and sex = #{userSex}
            </if>
        </where>
    </select>

    <!-- 根据queryvo中的Id集合实现查询用户列表 -->
    <select id="findUserInIds" resultMap="userMap" parameterType="queryvo">
        <include refid="defaultUser"></include>
        <where>
            <if test="ids != null and ids.size()>0">
                <foreach collection="ids" open="and id in (" close=")" item="uid" separator=",">
                    #{uid}
                </foreach>
            </if>
        </where>
    </select>
</mapper>
<? xml Version = "1.0" encoding = "UTF-8"?> 
<! DOCTYPE the Configuration 
        the PUBLIC "- // mybatis.org//DTD Config 3.0 // EN" 
        "http://mybatis.org/dtd/mybatis config.dtd--3 "> 
<configuration> 
    <-! configuration Properties -> 
    <Properties Resource =" jdbcConfig.properties "> </ Properties> 

    <-! typeAliases configuration using an alias, it is only the configuration of the domain class alias -> 
    <typeAliases> 
        < Package name = "com.itheima.domain"> </ Package > 
    </ typeAliases> 

    <-! configuration environment -> 
    <environments default = "MySQL">
        <! - Configure mysql environment -> 
        <Environment the above mentioned id = "mysql"> 
            <! - Configuration Services ->
            <transactionManager type="JDBC"></transactionManager>

            <!--配置连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"></property>
                <property name="url" value="${jdbc.url}"></property>
                <property name="username" value="${jdbc.username}"></property>
                <property name="password" value="${jdbc.password}"></property>
            </dataSource>
        </environment>
    </environments>
    <!-- 配置映射文件的位置 -->
    <mappers>
        <package name="com.itheima.dao"></package>
    </mappers>
</configuration>
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/eesy_mybatis
jdbc.username=root
jdbc.password=1234
package com.itheima.test;

import com.itheima.dao.IUserDao;
import com.itheima.domain.QueryVo;
import com.itheima.domain.User;
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 org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author horse programmer 
 * @Company http://www.ithiema.com 
 * 
 * mybatis test operation of crud 
 * / 
public  class MybatisTest { 

    Private the InputStream in;
     Private the SqlSession SQLSESSION;
     Private IUserDao userDao; 

    @Before // for before testing method of performing 
    public  void the init () throws Exception {
         // 1. reads the configuration file to generate an input stream of bytes 
        in Resources.getResourceAsStream = ( "the SqlMapConfig.xml" );
         // 2. Get a SqlSessionFactory 
        a SqlSessionFactory Factory = new newThe SqlSessionFactoryBuilder () Build (in);.
         // 3. The SqlSession acquired 
        SQLSESSION = factory.openSession ( to true );
         // 4. Get the proxy object dao 
        userDao = sqlSession.getMapper (IUserDao. Class ); 
    } 

    @After // after the test method for performing execution 
    public  void the destroy () throws Exception {
         // commit the transaction
        // sqlSession.commit ();
         // 6. the release resources 
        sqlSession.close (); 
        in.close (); 
    } 

    / ** 
     * test all queries 
     * / 
    @Test 
    public void testFindAll () {
         // 5. The method of performing Search 
        List <the User> Users = userDao.findAll ();
         for (the User User: Users) { 
            System.out.println (User); 
        } 

    } 


    / ** 
     * Delete Test operation 
     * / 
    @Test 
    public  void testFindOne () {
         // 5. The method of performing a query 
        the User User = userDao.findById (50 ); 
        System.out.println (User); 
    } 

    / ** 
     * test Fuzzy query 
     * / 
    @ the Test 
    public  void testFindByName () {
         // 5. The method of performing a query
        List <User> users = userDao.findByName ( "% King%" );
 //         List <the User> Users = userDao.findByName ( "King"); 
        for (the User User: Users) { 
            System.out.println (User) ; 
        } 
    } 

    / ** 
     * test QueryVo used as a query 
     * / 
    @Test 
    public  void testFindByVo () { 
        QueryVo VO = new new QueryVo (); 
        the User User = new new the User (); 
        user.setUserName ( "King%%" ); 
        vo.setUser (User); 
        // 5. The method of performing a query 
        List <User> users =userDao.findUserByVo (VO);
         for (U the User: Users) { 
            System.out.println (U); 
        } 
    } 

    / ** 
     * Search Test 
     * / 
    @Test 
    public  void testFindByCondition () { 
        the User U = new new the User () ; 
        u.setUserName ( "Pharaoh" );
 //         u.setUserSex ( "F"); 

        // 5. All methods execute the query 
        List <the User> = the Users userDao.findUserByCondition (U);
         for (the User the User: the Users) { 
            System.out.println (User); 
        } 

    } 


    / **
     * Test foreach tag use 
     * / 
    @Test 
    public  void testFindInIds () { 
        QueryVo VO = new new QueryVo (); 
        List <Integer> = List new new the ArrayList <Integer> (); 
        List.add ( 41 is ); 
        List.add ( 42 is ); 
        List.add ( 46 is ); 
        vo.setIds (List); 


        // 5. The method of performing Search 
        List <the User> Users = userDao.findUserInIds (VO);
         for (the User User: Users) { 
            System.out.println (User); 
        } 

    } 
}

 

Guess you like

Origin www.cnblogs.com/yangshuyuan1009/p/11482165.html