20201216 王维

学习总结

1 mybatis配置

dao接口映射文件

<?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 映射的接口 -->
<mapper namespace="com.itlaobing.ssm.dao.IUserDao">
    <!-- resultType 查询结果的映射类型 -->
    <select id="findAll" resultType="com.itlaobing.ssm.model.UserInfo">
        select * from users
    </select>
    <select id="findAllLimit" resultType="com.itlaobing.ssm.model.UserInfo" parameterType="com.itlaobing.ssm.util.PageUtil">
        select * from users
        <where>
            <if test="search != null and search != ''">
                username like '%${search}%'
                or phone like '%${search}%'
                or email like '%${search}%'
            </if>
        </where>
        <if test="sort != null and sort != ''">
            order by ${sort}
        </if>
        <if test="order != null and sort != ''">
            ${order}
        </if>
    </select>
</mapper>

mybatis全局配置

<?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>

    <!-- 读数据库连接文件 -->
    <properties resource="db.properties" />

    <!-- 别名 -->
    <typeAliases>
        <package name="com.itlaobing.ssm.model"/>
    </typeAliases>

    <!-- 数据源环境 -->
    <environments default="mysql">
        <environment id="mysql">
            <!-- 使用JDBC事务 -->
            <transactionManager type="JDBC"></transactionManager>
            <!-- 连接池类型 -->
            <dataSource type="pooled">
                <!-- 连接属性 -->
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${user}"/>
                <property name="password" value="${pass}"/>
            </dataSource>
        </environment>
    </environments>

    <!-- 扫描代理的接口 -->
    <mappers>
        <package name="com.itlaobing.ssm.dao"/>
    </mappers>

</configuration>

test测试

package com.itlaobing.ssm.dao;

import com.itlaobing.ssm.model.UserInfo;
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.Test;


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


public class IUserDaoTest {

    @Test
    public void findAll() throws IOException {
        //读取mybatis的配置信息
        InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
        //创建代理工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        //创建连接
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //创建代理对象
        IUserDao userInfo = sqlSession.getMapper(IUserDao.class);
        //执行方法
        List<UserInfo> list = userInfo.findAll();
        //遍历结果集
        for (UserInfo user : list) {
            System.out.println(user);
        }
    }
}

2 spring配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 扫包 将注解过的类使用spring容器管理 -->
    <context:component-scan base-package="com.itlaobing.ssm"/>

    <!-- 读数据库的连接信息 -->
    <context:property-placeholder location="classpath:db.properties"/>

    <!--创建数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${user}"/>
        <property name="password" value="${pass}"/>
    </bean>

    <!-- 配置mybatis的session工厂 将他托管给spring容器 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 别名 -->
        <property name="typeAliasesPackage" value="com.itlaobing.ssm.model"/>
        <!-- 分页插件 -->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <!-- 这里的几个配置主要演示如何使用,如果不理解,一定要去掉下面的配置 -->
                    <property name="properties">
                        <value>
                            <!--PageHelper默认情况下会检测到当前的数据库URL,自动选择相应的数据库方言。
                            您可以配置helperDialect属性以指定方言。您可以使用下面的缩写:
oracle,mysql,mariadb,sqlite,hsqldb,postgresql, db2,sqlserver,informix,h2,sqlserver2012,derby。 -->
                            helperDialect=mysql
                            <!-- 分页参数合理化,默认值为false。当此参数设置为时true,
                            pageNum <= 0将查询第一页 PageNum> pages(超过总数)
                            ,将查询最后一页。Default false,直接基于参数查询-->
                            reasonable=true
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

    <!-- 配置 Mapper 扫描器-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
        <property name="basePackage" value="com.itlaobing.ssm.dao"/>
    </bean>
</beans>

test测试

package com.itlaobing.ssm.service;

import com.itlaobing.ssm.model.UserInfo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

import static org.junit.Assert.*;

//注解的方式
//spring的测试环境
//@RunWith(SpringJUnit4ClassRunner.class)
//spring的配置文件
//@ContextConfiguration("classpath:spring.xml")
public class IUserServiceTest {

    @Autowired
    private IUserService service;

    @Test
    public void findAll() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        service = applicationContext.getBean(IUserService.class);
        List<UserInfo> list = service.findAll();
        System.out.println("-------------------------------------------------");
        for (UserInfo user : list) {
            System.out.println(user);
        }
    }
}

3 springmvc配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 扫包 将注解过的类使用spring容器管理 -->
    <context:component-scan base-package="com.itlaobing.ssm"/>

    <!-- 读数据库的连接信息 -->
    <context:property-placeholder location="classpath:db.properties"/>

    <!--创建数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${user}"/>
        <property name="password" value="${pass}"/>
    </bean>

    <!-- 配置mybatis的session工厂 将他托管给spring容器 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 别名 -->
        <property name="typeAliasesPackage" value="com.itlaobing.ssm.model"/>
        <!-- 分页插件 -->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <!-- 这里的几个配置主要演示如何使用,如果不理解,一定要去掉下面的配置 -->
                    <property name="properties">
                        <value>
                            <!--PageHelper默认情况下会检测到当前的数据库URL,自动选择相应的数据库方言。
                            您可以配置helperDialect属性以指定方言。您可以使用下面的缩写:
oracle,mysql,mariadb,sqlite,hsqldb,postgresql, db2,sqlserver,informix,h2,sqlserver2012,derby。 -->
                            helperDialect=mysql
                            <!-- 分页参数合理化,默认值为false。当此参数设置为时true,
                            pageNum <= 0将查询第一页 PageNum> pages(超过总数)
                            ,将查询最后一页。Default false,直接基于参数查询-->
                            reasonable=true
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

    <!-- 配置 Mapper 扫描器-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
        <property name="basePackage" value="com.itlaobing.ssm.dao"/>
    </bean>

    <!-- springmvc配置 -->
    <!-- 配置映射器和适配器 -->
    <mvc:annotation-driven />

    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/pages/"/>
        <!-- 后缀 -->
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
</web-app>

心得体会

完善了权限管理的页面内容,对各种细节进行了一定的处理,再次练习了ssm框架的整合,对很多遗忘的配置进行了复习。

标签

评论


© 2021 成都云创动力科技有限公司 蜀ICP备20006351号-1