ssm+tymeleaf整合

web.xml配置

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!--过滤器:设置请求和响应的编码方式-->
  <filter>
    <filter-name>encodingFilter</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>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <!--初始化spring-mybatis.xml配置文件-->
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mybatis.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>


spring-mybatis.xml

<?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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--扫描所有包-->
    <context:component-scan base-package="com.test"></context:component-scan>
    <!--引入spring-mvc.xml配置-->
    <import resource="classpath:spring-mvc.xml"/>
    <!--连接池配置(数据源)-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/db_wic_book_manager"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--数据源-->
        <property name="dataSource" ref="dataSource"></property>
        <!--dao 接口对应的mapper配置文件-->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
        <!--配置mybatis-config:实体类别名,sql打印-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>
    <!--dao接口没有实现类,需要配置对应的mapper处理器
        配置扫描dao接口包,动态实现dao接口,并且注入到spring容器中
    -->
    <bean id="mapperScan" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--dao接口的位置-->
        <property name="basePackage" value="com.test.dao"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    <!--事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--开启声明式注解管理事务-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

spring-mvc.xml

<?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: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/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">

    <!--扫描controller包-->
    <context:component-scan base-package="com.test.controller"></context:component-scan>
    <!--过滤静态资源-->
    <mvc:default-servlet-handler/>
    <!--注解驱动标签:使访问路径跟后台方法匹配通过注解完成
        可以解决添加静态资源释放标签之后导入controller访问不到的问题
    -->
    <mvc:annotation-driven>
        <!--配置转换器-->
        <!--
        register-defaults="true"将配置的bean设置为默认的,就不需要spring帮助我们创建了
        -->
        <mvc:message-converters register-defaults="true">
            <!--fastjson的转换器实体类-->
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <!--
                    1. 设置反返回数据格式是json格式,编码为utf-8
                    2. 避免ie浏览器执行异步请求时,返回json出现文件下载的现象
                -->
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=utf-8</value>
                        <value>text/html;charset=utf-8</value>
                    </list>
                </property>
                <!--下面这个可以控制返回数据的格式-->
                <property name="features">
                    <list>
                        <!--是否输出为null的字段,默认不输出,添加此属性之后可以输出-->
                        <value>WriteMapNullValue</value>
                        <!--当数值为null时,显示0-->
                        <value>WriteNullNumberAsZero</value>
                        <!--当字符串为null时,显示"" 空字符串-->
                        <value>WriteNullStringAsEmpty</value>
                        <!--当集合属性为null时,显示空数组-->
                        <value>WriteNullListAsEmpty</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <!--屏蔽解析器-->
    <mvc:resources mapping="/layui/**" location="/WEB-INF/layui/"></mvc:resources>
    <mvc:resources mapping="/css/**" location="/WEB-INF/css/"></mvc:resources>
    <mvc:resources mapping="/js/**" location="/WEB-INF/js/"></mvc:resources>
    <mvc:resources mapping="/img/**" location="/WEB-INF/img/"></mvc:resources>
    <mvc:resources mapping="/InModel/**" location="/WEB-INF/InModel/"></mvc:resources>
    <!--thymeleaf的模板解析器-->
    <bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".html"></property>
        <property name="characterEncoding" value="utf-8"></property>
        <!--设置视图解析器中查询链的顺序-->
        <property name="order" value="1"></property>
        <!--模板格式-->
        <property name="templateMode" value="HTML5"></property>
        <!--是否开启页面缓存-->
        <property name="cacheable" value="false"></property>
    </bean>
    <!--模板引擎-->
    <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver"></property>
    </bean>
    <!--视图解析器-->
    <bean id="viewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="characterEncoding" value="utf-8"></property>
        <property name="templateEngine" ref="templateEngine"></property>
    </bean>
    <!--配置上传文件解析器-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--最大上传大小   单位是b-->
        <property name="maxUploadSize" value="100000000"></property>
        <!--最大上传内存大小-->
        <property name="maxInMemorySize" value="100000000"></property>
    </bean>
</beans>

mybatis-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>
    <settings>
        <!--打印sql语句-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <typeAliases>
        <!--
            1. 给某类型设置一个别名
        -->
        <!--<typeAlias type="com.qianfeng.pojo.Student" alias="Student"></typeAlias>-->
        <!--
            2. 给整个包进行配置,在mapper使用时可以直接使用类名
        -->
        <package name="com.test.pojo"></package>
    </typeAliases>
</configuration>

generatorConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
    <!--配置文件-->
    <properties resource="config.properties"/>
    <!--产生最简单的sql  CRUD-->
    <context id="Mysql" targetRuntime="MyBatis3simple">
    <!--<context id="Mysql" targetRuntime="MyBatis3">-->
        <!--产生序列化  hashcode    toString 方法-->
        <!--<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>-->
        <!--<plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>-->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--数据库的连接-->
        <jdbcConnection driverClass="${driverClassName}"
                        connectionURL="${jdbc_url}" userId="${jdbc_username}"
                        password="${jdbc_password}"/>

        <!--指定生成的类型为java类型,避免数据库中number等类型字段 false时解析成Integer  true时解析成BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 指定生成pojo的包和此包在项目中的地址; -->
        <javaModelGenerator targetPackage="com.test.pojo"
                            targetProject="src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="true"/>
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 指定生成pojo的映射xml文件的所在包和此包在项目中的地址 -->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- 指定生成dao接口的所在包,以及包在项目中的地址 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.test.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>


        <!-- 配置表名跟pojo名  该table节点可以多个 -->
        
        <table tableName="tb_book" domainObjectName="Book"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
       
    </context>
</generatorConfiguration>

config.properties数据库连接

driverClassName=com.mysql.jdbc.Driver
validationQuery=SELECT 1
jdbc_url=jdbc:mysql://localhost:3306/db_wic_book_manager
jdbc_username=root
jdbc_password=root

pom.xml导入依赖

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.book</groupId>
  <artifactId>Book</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>book_manager Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!--mybatis逆向工程的依赖-->
    <dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-core</artifactId>
      <version>1.3.7</version>
    </dependency>
    <!--spring管理对象的依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
    <!--导入spring-context的依赖(spring上下文)-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
    <!--导入spring-webmvc的依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
    <!--导入mybati依赖-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.6</version>
    </dependency>
    <!--导入mysql的依赖-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.38</version>
    </dependency>
    <!--导入mybatis和spring的整合包-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.2</version>
    </dependency>
    <!--druid连接池-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.10</version>
    </dependency>
    <!--mybatis的事务使用jdbc的事务-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
    <!--spring事务管理的依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
    <!--引入fastjson的依赖-->
    <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.58</version>
    </dependency>
    <!--上传文件的依赖-->
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
    </dependency>
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.3</version>
    </dependency>
    <!--导入servlet-api的依赖,解决request response无法使用的问题-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <!--
        provided:代表只在编译和测试的时候使用
      -->
      <scope>provided</scope>
    </dependency>
    <!--引入thymeleaf的依赖-->
    <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf</artifactId>
      <version>3.0.9.RELEASE</version>
    </dependency>
    <!--导入thymeleaf跟spring整合的依赖-->
    <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf-spring4</artifactId>
      <version>3.0.9.RELEASE</version>
    </dependency>
    
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.2</version>
        <!--设置配置文件的位置-->
        <configuration>
          <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
          <overwrite>true</overwrite>
          <verbose>true</verbose>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>

  </build>
</project>

添加mybatis

在这里插入图片描述保存运行,生成Mapper,逆向pojo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值