SSM框架及动态sql

1.SSM框架搭建

1.1 SpringMVC搭建

(1)pom.xml之中添加war
(2)main底下新建webapp文件夹,webapp下新建WEB-INF文件夹
(3)生成web.xml:
File——>Project Structure——>Facets——>Web——>添加web.xml(选择生成位置)
(4)配置SpringMVC
(5)添加tomcat启动,检测mvc是否正常运行

1.2 Mybatis搭建

(1)数据源

<!--数据源配置-->
<util:properties id="jdbc" location="classpath:jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" >
    <property name="url" value="#{jdbc.url}" />
    <property name="driverClassName" value="#{jdbc.driver}" />
    <property name="username" value="#{jdbc.username}" />
    <property name="password" value="#{jdbc.password}" />
    <property name="maxIdle" value="#{jdbc.maxIdle}" />
</bean>

(2)sqlSessionFactory配置(数据源,mapper扫描,别名)

<!--mybatis-2.创建sqlSessionFactory工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath:mapper/*Mapper.xml" />
<!--    配置别名-->
        <property name="typeAliasesPackage" value="cn.goktech.entity"/>
    </bean>

(3)MapperScannerConfigurer配置(扫描DAO)

<!--mybatis-3.配置MapperScannerConfigurer-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--       指定要扫描的包-->
        <property name="basePackage" value="cn.goktech.dao"/>
    </bean>

2.动态sql

if标签
where标签

<!--    if标签-->
    <select id="testIf" parameterType="User" resultType="User">
        select * from user
        <!--where标签,当内部无成立条件时,where条件不拼接 -->
        <where>
            <if test="name!=null">
                name like concat('%',#{name},'%')
            </if>
            <if test="age!=0">
                and age>#{age}
            </if>
            <if test="phone!=null">
                and phone like concat('%',#{phone},'%')
            </if>
        </where>
    </select>

choose标签

<!--    choose when otherwise标签,自上而下判断进入哪个分支,且只能进入一个-->
    <select id="testChoose" parameterType="User" resultType="User">
        select * from user where
        <choose>
            <when test="id!=0">
                id=#{id}
            </when>
            <when test="name!=null">
                name=#{name}
            </when>
            <otherwise>
                age=21
            </otherwise>
        </choose>
    </select>

set标签

<!--set标签-->
    <update id="testSet" parameterType="User">
        update user
        <set>
            <if test="name!=null">
                name=#{name},
            </if>
            <if test="age!=0">
                age=#{age},
            </if>
            <if test="info!=null">
                info=#{info}
            </if>
        </set>
        where id=#{id}
    </update>

trim标签

<!--    trim标签,prefix子语句的前缀,suffix子语句的后缀,prefixOverrides子语句首部内容过滤,suffixOverrides子语句尾部内容过滤-->
    <update id="testTrim" parameterType="User">
        update user
        <trim prefix="set" suffix="where id=#{id}" suffixOverrides=","> <!--如果后缀出现了逗号,就把他覆盖掉 -->
            <if test="name!=null">
                name=#{name},
            </if>
            <if test="age!=0">
                age=#{age},
            </if>
            <if test="info!=null">
                info=#{info}
            </if>
        </trim>
    </update>

foreach标签

<!--测试foreach标签,open集合拼接以什么开始,close以什么结束,item元素迭代别名,collection:list,arrayList,collection-->
<!--   遍历Map时,给定@Param-->
    <select id="testForEach" resultType="User">
    <!-- (1001,1002,1005,1004)-->
        select * from user where id in
        <foreach collection="list" item="award" open="(" close=")" separator=",">
            #{award}
        </foreach>
    </select>

3.Mybatis分页插件

(1)pom.xml添加:

<!--   mybatis分页插件-->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.2</version>
</dependency>

(2)sqlSessionFactory中添加配置

<!--   配置分页插件 -->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <value>
                            helperDialect=mysql
                        </value>
                    </property>
                </bean>
            </array>
        </property>

(3)使用

@RequestMapping("/testPageHelper")
    public String testPageHelper(){
//        这一句必须写到查询getAll()前面
        PageHelper.startPage(2,3);
//        先给定条件,再查询
        List<User> uList = userDao.getAll();
        PageInfo<User> pu = new PageInfo<User>(uList);
        List<User> list = pu.getList();
        for (User u:list){
            System.out.println(u);
        }
        System.out.println("总记录"+pu.getTotal());
        System.out.println("总页数"+pu.getPages());
        return "index";
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值