Training summary day5

Integration SSM

SSM steps to achieve integration login:

  1. Import all the jar package
  2. Add all configuration files, convert config folder for the resource folder
  3. Add character encoding filter in web.xml, registered springmvc and spring framework
  4. Realize the function of all profiles
  5. Mybatis use reverse engineering to automatically generate pojo, generate mapper interfaces and file mapper.xml
  6. New service interface and implementation class, completion administrator login
  7. New controller, complete the login function
  8. Add pages, images, CSS
  9. Rectification html page to jsp page
  10. Test function

jar package:
Here Insert Picture Description
Profile:
jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/xiaomissm?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456

applicationContext-dao.xml

<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"
   xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-4.0.xsd 
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
      http://www.springframework.org/schema/task
          http://www.springframework.org/schema/task/spring-task-4.0.xsd
      http://code.alibabatech.com/schema/dubbo">
   <!--读取属性文件-->
   <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
   <!--配置数据源-->
   <bean id = "dataSource" class="com.alibaba.druid.pool.DruidDataSource">
      <property name="driverClassName" 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>
   </bean>
   <!--配置mybatis工厂-->
   <bean class="org.mybatis.spring.SqlSessionFactoryBean">
      <!--工厂中要用到的数据源-->
      <property name="dataSource" ref="dataSource"></property>
      <!--配置mybatis的配置文件-->
      <property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
      <!--配置pojo-->
      <property name="typeAliasesPackage" value="com.oracle.xiaomi.pojo"></property>
   </bean>
   <!--配置mapper所在的包-->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="basePackage" value="com.oracle.xiaomi.mapper"
   </bean>
</beans>

applicationContext-service.xml

<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"
   xmlns:aop="http://www.springframework.org/schema/aop" 
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:task="http://www.springframework.org/schema/task"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-4.0.xsd 
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
      http://www.springframework.org/schema/task
          http://www.springframework.org/schema/task/spring-task-4.0.xsd
      http://code.alibabatech.com/schema/dubbo">
      
   <!--配置业务逻辑层,给spring扫描识别该包下的所有类,使用注解方式-->
   <context:component-scan base-package="com.oracle.xiaomi.service"></context:component-scan>
</beans>

springmvc.xml

<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"
   xmlns:aop="http://www.springframework.org/schema/aop" 
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:task="http://www.springframework.org/schema/task"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-4.0.xsd 
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
      http://www.springframework.org/schema/task
          http://www.springframework.org/schema/task/spring-task-4.0.xsd
      http://code.alibabatech.com/schema/dubbo">
   <!--扫描根包 -->
   <context:component-scan base-package="com.oracle.xiaomi.controller"></context:component-scan>
   <!--视图解析器-->
<bean id="ResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="prefix" value="/admin/"></property>
   <property name="suffix" value=".jsp"></property>
</bean>
   <!--注册文件上传插件-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
   <!--注解JSON驱动-->
   <mvc:annotation-driven></mvc:annotation-driven>
</beans>

SqlMapConfig.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>
    <!--注册分页插件-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
        </plugin>
    </plugins>
</configuration>

Use of reverse engineering mybatis:
Mybatis Reverse Engineering used to automatically generate the corresponding database table POJO, and generating an interface mapper mapper.xml file, a key to complete the entire process, eliminating the need for the use of the process we prepared mybatis sql mapping framework.
Mybatis reverse engineering the main configuration file 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>
   <context id="testTables" targetRuntime="MyBatis3">
      <commentGenerator>
         <!-- 是否去除自动生成的注释 true:是 : false:否 -->
         <property name="suppressAllComments" value="true" />
      </commentGenerator>
      <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
      <jdbcConnection driverClass="com.mysql.jdbc.Driver"
         connectionURL="jdbc:mysql://localhost:3306/xiaomissm" userId="root"
         password="123456">
      </jdbcConnection>
      <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 
         NUMERIC 类型解析为java.math.BigDecimal -->
      <javaTypeResolver>
         <property name="forceBigDecimals" value="false" />
      </javaTypeResolver>
      <!-- targetProject:生成PO类的位置 -->
      <javaModelGenerator targetPackage="com.oracle.xiaomi.pojo"
         targetProject=".\src">
         <!-- enableSubPackages:是否让schema作为包的后缀 -->
         <property name="enableSubPackages" value="false" />
         <!-- 从数据库返回的值被清理前后的空格 -->
         <property name="trimStrings" value="true" />
      </javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
      <sqlMapGenerator targetPackage="com.oracle.xiaomi.mapper" 
         targetProject=".\src">
         <!-- enableSubPackages:是否让schema作为包的后缀 -->
         <property name="enableSubPackages" value="false" />
      </sqlMapGenerator>
      <!-- targetPackage:mapper接口生成的位置 -->
      <javaClientGenerator type="XMLMAPPER"
         targetPackage="com.oracle.xiaomi.mapper" 
         targetProject=".\src">
         <!-- enableSubPackages:是否让schema作为包的后缀 -->
         <property name="enableSubPackages" value="false" />
      </javaClientGenerator>
      <!-- 指定数据库表 -->
      <table schema="" tableName="admin"></table>
      <table schema="" tableName="product_type"></table>
      <table schema="" tableName="product_info"></table>
   </context>
</generatorConfiguration>
Published 46 original articles · won praise 16 · views 2637

Guess you like

Origin blog.csdn.net/qq_43598138/article/details/103837149