mybatis database connection pool and sql advanced

(1) The connection pool is a container used to store connections. The container is a collection object, which must be thread-safe. Two threads cannot get the same connection. The collection has a queue feature: first in, first out

(2) Mybatis connection pool provides 3 configuration methods

         The main configuration file is in the Maven project, under the resources file, the name is: the DataSource tag in SqlMapConfig.xml, the value of the type attribute is:

                        POOLED: Using the connection pool in the traditional javax.sql.DataSource specification, mybatis has a specification-specific implementation

                       UNPOOLED: Using the traditional way of obtaining connections, although the javax.sql.DataSource interface is also implemented, the idea of ​​pooling is not implemented

                       JNDI: The JNDI technology provided by the server is used to obtain DataSource objects. Different servers can get different DataSource objects.

                                                                If it is not a web or Maven jar project, it cannot be used

  (3) <mapper namespace="cn.rzpt.dao.IUserDao">: Write the namespace corresponding to whose Dao

(4) The xml writing if tag of incoming unknown parameters:

<mapper namespace="cn.rzpt.dao.IUserDao">
<!--在这里,使用if进行判断时,只能使用and符号,不能是两个与符号-->
    <select id="findUserByCondition" resultType="cn.rzpt.domain.User" parameterType="cn.rzpt.domain.User">
        select * from user where 1=1
        <if test="username != null">
            and username = #{username}
<!--这里面需要区分大小写,要与实体类的写法一致-->
        </if>
       <if test="sex != null">
           and sex = #{sex}
       </if>
    </select>
</mapper>

where tag:

<select id="findUserByCondition" resultType="cn.rzpt.domain.User" parameterType="cn.rzpt.domain.User">
    select * from user
    <where>
        <if test="username != null">
            and username = #{username}
        </if>
        <if test="sex != null">
            and sex = #{sex}
        </if>
    </where>
</select>

Need to write VO:

public class QueryVO {
    private User user;

    public User getUser() {
        return user;
    }

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

interface

List<User> findUserByVo(QueryVO vo);

//根据传入的参数条件查询:姓名,性别,地址...,有其一,其二等,或者全部
List<User> findUserByCondition(User user);

Test class

@Test
public void testFindByCondition(){
    User user = new User();
    user.setUsername("老王");
    List<User> users = userDao.findUserByCondition(user);

    for (User u : users){
        System.out.println(u);
    }
}

(5) foreach: pass in multiple parameters, id in (...)

<mapper namespace="cn.rzpt.dao.IUserDao"> 
    <!-- Configure the correspondence between the column name of the query result and the attribute name of the entity class --> 
    <resultMap id="userMap" type="cn.rzpt.domain .User"> 
        <!-- Correspondence of primary key field--> 
        <id property="id" column="id"></id> 
        <!-- Correspondence of non-primary key field, the property should be consistent with the entity class attribute, The column must be consistent with the database. Under Linux, the column name is case sensitive --> 
        <result property="username" column="username"></result> 
        <result property="address" column="address"></ result> 
        <result property="sex" column="sex"></result> 
        <result property="birthday" column="birthday"></result>
    </resultMap> 

    <!--Extract repeated sql statements--> 
    <sql id="defaultUser"> 
        <!--If there are other statements following, don't write a semicolon at this time, or an error will be reported -->  
        select * from user
    </sql>

    <!--根据queryvo中的id集合实现查询用户列表-->
    <select id="findUserInIds" resultMap="userMap" parameterType="cn.rzpt.domain.QueryVO">
        <!--select * from user-->
        <include refid="defaultUser"></include>
        <where>
            <if test="ids != null and ids.size()>0">
                <foreach collection="ids" open="and id in(" close=")" item="id" separator=",">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>


</mapper>

 

public class QueryVO {
    private User user;

    private List<Integer> ids;

    public List<Integer> getIds() {
        return ids;
    }

    public void setIds(List<Integer> ids) {
        this.ids = ids;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
Added in public interface IUserDao
/** 
 * Query user information according to the id set provided in queryvo 
 * @param vo 
 * @return 
 */ 
List<User> findUserInIds(QueryVO vo);

 

Test class

@Test
public void testFindInIds(){
    QueryVO vo = new QueryVO();
    List<Integer> list = new ArrayList<Integer>();
    list.add(41);
    list.add(43);
     vo.setIds(list);

    List<User> users = userDao.findUserInIds(vo);

    for (User u : users){
        System.out.println(u);
    }
}

Guess you like

Origin blog.csdn.net/sky2line/article/details/109349941