使用SSM重构Bookstore——环境准备

1、系统环境

使用SSM重构Bookstore——环境准备_第1张图片

2、数据库建表

启动mysql服务

sudo systemctl start mysqld

登录mysql

mysql -u root -p

建立数据库(如果存在先删除)

drop database if exists `Bookstore`;
create database `Bookstore`;
use Bookstore;

建表(建好后通过show create table 表名 拷贝过来的,自己写的有点乱,注意字符集选取utf8mb4而不是utf8)

  • 用户表
CREATE TABLE `tbl_User` (
  `id` varchar(100) NOT NULL,
  `username` varchar(20) NOT NULL,
  `password` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `telephone` varchar(20) NOT NULL,
  `registTime` timestamp NOT NULL,
  `realname` varchar(10) DEFAULT NULL,
  `gender` char(1) DEFAULT 'M',
  `birthday` date DEFAULT NULL,
  `activeCode` varchar(50) DEFAULT NULL,
  `state` tinyint(4) DEFAULT NULL,
  `role` varchar(10) DEFAULT 'normal', 
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; 
  • 产品表
CREATE TABLE `tbl_Product` (
  `id` varchar(100) NOT NULL,
  `name` varchar(40) NOT NULL,
  `price` double NOT NULL,
  `category` varchar(40) NOT NULL,
  `pnum` bigint(20) NOT NULL,
  `imgurl` varchar(100) DEFAULT NULL,
  `description` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  • 订单表
 CREATE TABLE `tbl_Order` (
  `id` varchar(100) NOT NULL,
  `money` double NOT NULL,
  `receiverAddress` varchar(255) NOT NULL,
  `receiverName` varchar(20) NOT NULL,
  `receiverPhone` varchar(20) NOT NULL,
  `paystate` tinyint NOT NULL,
  `ordertime` timestamp NOT NULL,
  `user_id` varchar(100) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`),
  CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `tbl_User` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  • 订单条目表
CREATE TABLE `tbl_OrderItem` (
  `order_id` varchar(100) NOT NULL,
  `product_id` varchar(100) NOT NULL,
  `buynum` int(11) DEFAULT NULL,
  PRIMARY KEY (`order_id`,`product_id`),
  KEY `product_id` (`product_id`),
  CONSTRAINT `orderitem_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `tbl_Order` (`id`),
  CONSTRAINT `orderitem_ibfk_2` FOREIGN KEY (`product_id`) REFERENCES `tbl_Product` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

3、 SSM环境配置

IDE
使用SSM重构Bookstore——环境准备_第2张图片 1. 使用Maven新建工程
使用SSM重构Bookstore——环境准备_第3张图片2. 完善目录结构
使用SSM重构Bookstore——环境准备_第4张图片
3. 配置依赖 —— POM文件(直接在Idea的Project Structure中配置依赖在刷新POM时会被丢弃)



<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.0modelVersion>

  <groupId>ssmgroupId>
  <artifactId>BookstoreartifactId>
  <version>1.0-SNAPSHOTversion>
  <packaging>warpackaging>

  <name>Bookstore Maven Webappname>
  <url>http://localhost:8080/Bookstoreurl>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.8maven.compiler.source>
    <maven.compiler.target>1.8maven.compiler.target>
      
      
    <spring-version>5.1.6.RELEASEspring-version>
  properties>

    
    <dependencies>

      
      <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-beansartifactId>
        <version>${spring-version}version>
      dependency>
      <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-contextartifactId>
        <version>${spring-version}version>
      dependency>
      <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-coreartifactId>
        <version>${spring-version}version>
      dependency>
      <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-expressionartifactId>
        <version>${spring-version}version>
      dependency>

      
      <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-aopartifactId>
        <version>${spring-version}version>
      dependency>
      <dependency>
        <groupId>org.aspectjgroupId>
        <artifactId>aspectjweaverartifactId>
        <version>1.9.3version>
      dependency>

      
      <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webartifactId>
        <version>${spring-version}version>
      dependency>
      <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webmvcartifactId>
        <version>${spring-version}version>
      dependency>
      <dependency>
        <groupId>javax.servletgroupId>
        <artifactId>javax.servlet-apiartifactId>
        <version>4.0.1version>
        <scope>providedscope>
      dependency>
      <dependency>
        <groupId>javax.servletgroupId>
        <artifactId>jstlartifactId>
        <version>1.2version>
      dependency>

      
      <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-jdbcartifactId>
        <version>${spring-version}version>
      dependency>
      <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-txartifactId>
        <version>${spring-version}version>
      dependency>

      
      <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatisartifactId>
        <version>3.5.1version>
      dependency>
      <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatis-springartifactId>
        <version>2.0.1version>
      dependency>
      <dependency>
        <groupId>org.mybatis.generatorgroupId>
        <artifactId>mybatis-generator-coreartifactId>
        <version>1.3.7version>
      dependency>

      
      <dependency>
        <groupId>com.mchangegroupId>
        <artifactId>c3p0artifactId>
        <version>0.9.5.4version>
      dependency>
      <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <version>8.0.15version>
      dependency>

      
      <dependency>
        <groupId>log4jgroupId>
        <artifactId>log4jartifactId>
        <version>1.2.17version>
      dependency>

      
      <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>4.12version>
        <scope>testscope>
      dependency>

    dependencies>
	
    
  <build>
    <finalName>BookstorefinalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-clean-pluginartifactId>
          <version>3.1.0version>
        plugin>
        
        <plugin>
          <artifactId>maven-resources-pluginartifactId>
          <version>3.0.2version>
        plugin>
        <plugin>
          <artifactId>maven-compiler-pluginartifactId>
          <version>3.8.0version>
        plugin>
        <plugin>
          <artifactId>maven-surefire-pluginartifactId>
          <version>2.22.1version>
        plugin>
        <plugin>
          <artifactId>maven-war-pluginartifactId>
          <version>3.2.2version>
        plugin>
        <plugin>
          <artifactId>maven-install-pluginartifactId>
          <version>2.5.2version>
        plugin>
        <plugin>
          <artifactId>maven-deploy-pluginartifactId>
          <version>2.8.2version>
        plugin>
      plugins>
    pluginManagement>
  build>
project>

4.使用Mybatis generator生成相关文件

生成器配置文件generatorConfig.xml位于src/main/resources






<generatorConfiguration>

  <properties resource="db.properties" />
  <context id="bookstore" targetRuntime="MyBatis3">

    
    <commentGenerator>
      <property name="suppressAllComments" value="true" />
      <property name="suppressDate" value="true" />
    commentGenerator>

    
    <jdbcConnection driverClass="${jdbc.driver}"
                    connectionURL="${jdbc.url}"
                    userId="${jdbc.username}"
                    password="${jdbc.password}">
    jdbcConnection>

    
    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    javaTypeResolver>

    
    <javaModelGenerator targetPackage="com.bookstore.Domain" targetProject="./src/main/java">
      <property name="trimStrings" value="true" />
    javaModelGenerator>

    
    <sqlMapGenerator targetPackage="Mapper"  targetProject="./src/main/resources">
    sqlMapGenerator>

     
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.bookstore.Dao"  targetProject="./src/main/java">
    javaClientGenerator>

	
    <table tableName="tbl_User" domainObjectName="User" />
    <table tableName="tbl_Product" domainObjectName="Product"/>
    <table tableName="tbl_Order" domainObjectName="Order"/>
    <table tableName="tbl_OrderItem" domainObjectName="OrderItem"/>

  context>
generatorConfiguration>

使用Maven插件运行MBG


<dependency>
	<groupId>org.mybatis.generatorgroupId>
	<artifactId>mybatis-generator-maven-pluginartifactId>
	<version>1.3.7version>
dependency>


<plugins>
	<plugin>
		<groupId>org.mybatis.generatorgroupId>
		<artifactId>mybatis-generator-maven-pluginartifactId>
		<version>1.3.7version>
		<configuration>
			<configurationFile>
				
			configurationFile>
			<verbose>trueverbose>
			<overwrite>trueoverwrite>
		configuration>
		<dependencies>
			<dependency>
                
				<groupId>mysqlgroupId>
				<artifactId>mysql-connector-javaartifactId>
				<version>8.0.15version>
			dependency>
		dependencies>
	plugin>
plugins>

使用SSM重构Bookstore——环境准备_第5张图片

运行后生成结果:
使用SSM重构Bookstore——环境准备_第6张图片
5. 配置Spring及Spring MVC

Spring配置文件src/main/resources/spring.xml


<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"
       xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
       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 http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">

    
    <context:component-scan base-package="com.bookstore">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    context:component-scan>

    
    <context:property-placeholder location="classpath:db.properties" />
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    bean>

    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
        <property name="dataSource" ref="dataSource" />
    bean>

    
    <tx:annotation-driven />  

    
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource" />
        
        <property name="mapperLocations" value="classpath:Mapper/*.xml" />
        
        <property name="configuration" >
            <bean class="org.apache.ibatis.session.Configuration" >
                <property name="mapUnderscoreToCamelCase" value="true" />
            bean>
        property>
    bean>

    
    
    <mybatis:scan base-package="com.bookstore.Dao" />

    

beans>

Spring MVC配置文件 src/main/resources/spring-mvc.xml


<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:mvc="http://www.springframework.org/schema/mvc"
       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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    
    <context:component-scan base-package="com.bookstore.Controller" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    context:component-scan>

    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    bean>

    
    <mvc:annotation-driven />

    
    <mvc:default-servlet-handler />
beans>

web.xml配置文件src/main/webapp/WEB-INF/web.xml


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

    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:spring.xmlparam-value>
    context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    
    <filter>
        <filter-name>encodingUTF8filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodingUTF8filter-name>
        <url-pattern>/*url-pattern>   
    filter-mapping>

    
    <servlet>
        <servlet-name>dispatcherservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <load-on-startup>1load-on-startup>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring-mvc.xmlparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>dispatcherservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
web-app>

你可能感兴趣的:(JAVA,Spring)