Integration of CRUD4 mybatis

Package Penalty for com.itheima.dao; 

Import com.itheima.domain.Account;
 Import com.itheima.domain.AccountUser; 

Import java.util.List; 

/ ** 
 * @author dark horse programmer 
 * @Company HTTP: // the WWW. ithiema.com 
 * / 
public  interface IAccountDao { 

    / ** 
     * Search account, but also the acquired user account information currently belongs 
     * @return 
     * / 
    List <the account> the findAll (); 

    / ** 
     * query all accounts, and with user name and address information 
     * @return 
     * / 
    List <AccountUser> findAllAccount (); 
}
Package com.itheima.dao; 

Import com.itheima.domain.User; 

Import java.util.List; 

/ ** 
 * @author horse programmer 
 * @Company http://www.ithiema.com 
 * 
 * user's persistence Interface 
 * / 
public  interface IUserDao { 

    / ** 
     * queries all the users, all the acquired user account information 
     * @return 
     * / 
    List <the user> the findAll (); 


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


}
<?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.itheima.dao.IAccountDao">

    <!-- 定义封装account和user的resultMap -->
    <resultMap id="accountUserMap" type="account">
        <id property="id" column="aid"></id>
        <result property="uid" column="uid"></result>
        <result property="money" column="money"></result>
        <!-- 一对一的关系映射:配置封装user的内容-->
        <association property="user" column="uid" javaType="user">
            <id property="id" column="id"></id>
            <result column="username" property="username"></result>
            <result column="address" property="address"></result>
            <result column="sex" property="sex"></result>
            <result column="birthday" property="birthday"></result>
        </association>
    </resultMap>

    <!-- 查询所有 -->
    <select id="findAll" resultMap="accountUserMap">
        select u.*,a.id as aid,a.uid,a.money from account a , user u where u.id = a.uid;
    </select>

    <!--查询所有账户同时包含用户名和地址信息-->
    <select id="findAllAccount" resultType="accountuser">
        select a.*,u.username,u.address from account a , user u where u.id = a.uid;
    </select>

</mapper>
<?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.itheima.dao.IUserDao">

    <!-- 定义User的resultMap-->
    <resultMap id="userAccountMap" type="user">
        <id property="id" column="id"></id>
        <result property="username" column="username"></result>
        <result property="address" column="address"></result>
        <result property="sex" column="sex"> </ Result> 
        <-! map configuration user accounts in the set of objects ->
        <Property Result = "Birthday" column = "Birthday"> </ Result>
        <collection property="accounts" ofType="account">
            <id column="aid" property="id"></id>
            <result column="uid" property="uid"></result>
            <result column="money" property="money"></result>
        </collection>
    </resultMap>

    <!-- 查询所有 -->
    <select id="findAll" resultMap="userAccountMap">
        select * from user u left outer join account a on u.id = a.uid
    </select>

    <!-- 根据id查询用户 -->
    <select id="findById" parameterType="INT" resultType="user">
        select * from user where id = #{uid}
    </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 of the 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>

 

Guess you like

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