mybatis

2018-04-02  本文已影响0人  打死你的小白兔

mybiatis-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>
    <!-- 配置mysql数据库的连接环境 -->
    <environments default="mybiatis"><!-- 默认名可以任意,但要和其下的id一致 -->
        <environment id="mybiatis">
            <!-- JDBC - 这个类型直接全部使用 JDBC 的提交和回滚功能。它依靠使用连接的数据源来管理事务的作用域。 MANAGED - 这个类型什么不做 
                , 它从不提交 、 回滚和关闭连接 。 而是让窗口来管理事务的全部生命周期 。(比如说 Spring 或者 JAVAEE 服务器) -->
            <transactionManager type="JDBC"></transactionManager><!-- 
                一般用JDBC -->
            <dataSource type="POOLED"><!-- 一般用POOLED -->
                <!-- 据源类型有三种: UNPOOLED , POOLED , JNDI 。 UNPOOLED - 这个数据源实现只是在每次请求的时候简单的打开和关闭一个连接。虽然这有点慢,但作为一些不需要性能和立即响应的简单应用来说 
                    , 不失为一种好选择 。 POOLED - 这个数据源缓存 JDBC 连接对象用于避免每次都要连接和生成连接实例而需要的验证时间 。对于并发 WEB 
                    应用,这种方式非常流行因为它有最快的响应时间。 JNDI - 这个数据源实现是为了准备和 Spring 或应用服务一起使用,可以在外部也可以在内部配置这个数据源,然后在 
                    JNDI 上下文中引用它。这个数据源配置只需要两上属性: -->
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url"
                    value="jdbc:mysql://localhost:3306/qq?characterEncoding=utf-8" />
                <property name="username" value="root" />
                <property name="password" value="root" />
            </dataSource>
        </environment>
    </environments>
    <!-- 配置映射文件 -->
    <mappers>
        <mapper resource="com/xx/mapper/UserDaoMapper.xml"/>
    </mappers>
</configuration>

mapper.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">
<!--namespace命名空间如果和spring整合时要用接口全类名 -->
<mapper namespace="com.hw.dao.UserDao">
    <resultMap type="com.hw.entity.User" id="userinfo">
        <result column="id" property="id"/>
        <result column="uname" property="uname"/><!--column和property不能省  -->
        <result column="pwd" property="pwd"/>
        
    </resultMap>
    <insert id="saveUser"   parameterType="com.hw.entity.User"><!-- parameterType参数类型 -->
        insert into tab_user values(null,#{uname},#{pwd},#{js});
    </insert>
    
    <delete id="delUser" parameterType="int"><!-- 删除单个 -->
        delete from tab_user where id=#{id}
    </delete>
    
    <delete id="delAllUser" parameterType="string"><!--删除多个 -->
        delete from tab_user where id in
        <foreach collection="array" item="id" open="(" close=")"
            separator=",">
            #{id}
        </foreach>
    </delete>
    
    <update id="updateUser" parameterType="com.hw.entity.User">
        update tab_user
        <set>
            <choose>
                <when test="uname!=null">
                    uname=#{uname}
                </when>
                <when test="pwd!=null">
                    pwd=#{pwd}
                </when>
                <when test="js!=null">
                    js=#{js}
                </when>
            </choose>
        </set>
        where id=#{id}
    </update>
    
    <select id="list"  resultMap="userinfo">
    <!-- resultType="com.hw.entity.User"返回结果类型,也可以用 resultMap="userinfo" -->
        select * from tab_user
    </select>
    
    <select id="getUser" resultType="com.hw.entity.User"
        parameterType="int">
        select * from tab_user where id=#{id}
    </select>
    
    <select id="getCountUser" resultType="int">
        select count(id) from
        tab_user
    </select>
    
    <select id="ListPage" resultMap="userinfo"
        parameterType="map">
        <!-- 如果开发模糊查询时用#则调用 时需要:map.put("uname", "%张%");map.put("js", "%管理员%"); 
            select * from tab_user where uname like #{uname} and js like #{js} limit 
            #{start},#{end} -->
        select * from tab_user where 1=1
        <if test="uname!=null">
            and uname like '%${uname}%'
        </if>
        <if test="js!=null">
            and js like '%${js}%'
        </if>
        order by id desc limit #{start},#{end}
    </select>
    
    <select id="getCountListUser" parameterType="map" resultType="int">
        select count(id) from tab_user where 1=1
        <if test="uname!=null">
            and uname like '%${uname}%'
        </if>
        <if test="js!=null">
            and js like '%${js}%'
        </if>
    </select>

</mapper>

测试

//开发mybatis第1步读取mybatis配置文件
    Reader resourceAsReader = Resources.getResourceAsReader("config/mybatis-config.xml");
    //开发mybatis第2步创建mybatis的工厂模式:SqlSessionFactory 
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsReader);
    //开发mybatis第3步创建mybatis的session
    SqlSession session = factory.openSession();
    //开发mybatis第4步通过session操作数据库
    List<Dept> list=session.selectList("listDept");
    for(Dept dd:list){
        System.out.println(dd.getDid()+" "+dd.getDname());
        //if(dd.getDname().equals("开发部")){
            Set<Person> set = dd.getSet();
            for(Person p:set){
                System.out.println(p.getName()+ " "+p.getDegree());
            }
        //}
    }
    List<Person> list1=session.selectList("list");
    for(Person p:list1){
        System.out.println(p.getName()+ " "+p.getDegree());
    }
    //开发mybatis第5步执行事务提交(查询查以 不需要)
    session.commit();
    //开发mybatis第6步关闭连挡
    session.close();
上一篇 下一篇

猜你喜欢

热点阅读