idea+SSM+Mysql框架整合

 

  

一、整体步骤

maven建好web工程并配置好整体包架构(三层架构)->pom文件中所需依赖导入->配置web.xml->spring-mvc配置->配置spring-mybatis配置->建立相应代码和数据表进行测试

整体结构如下

idea+SSM+Mysql框架整合

 

 1.maven新建项目,在新建项目时加入一组东西,下载依赖会快

archetypeCatalog
internal

2.然后补充好java源包,resources配置包,三层架构包

3.配置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.yiyezhiqiu</groupId>
  <artifactId>ssm01</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>ssm01 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>

<!--配置servlet依赖-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.0</version>
      <scope>provided</scope>
    </dependency>
<!--配置spring框架依赖-->
    <!--配置spring-webmvc依赖-->

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>
<!--引入spring-jdbc依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>
<!--配置数据库的依赖-->
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.38</version>
  </dependency>
    <!--引入数据库连接技术-->
    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
    </dependency>
    <!--配置mybatis的依赖-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.6</version>
    </dependency>
    <!--配置spring和mybatis整合依赖-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.1</version>
    </dependency>
    <!--引入JSTL标签类依赖-->
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>


    <!--logback 日志处理-->
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.2.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core -->
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
      <version>1.2.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.logback-extensions/logback-ext-spring -->
    <dependency>
      <groupId>org.logback-extensions</groupId>
      <artifactId>logback-ext-spring</artifactId>
      <version>0.1.4</version>
    </dependency>


  </dependencies>
  <build>
    <finalName>ssm01</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

  3.配置web.xml文件(比如前端控制器DispatcherServlet)

 

<!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 xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
            http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

  <display-name>Archetype Created Web Application</display-name>

  <!-- Spring配置 -->
  <!-- ================================================= -->
  <!--配置Spring IoC的配置文件路径 classpath相当于resources文件夹-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-mybatis.xml</param-value>
  </context-param>
  <!--Log4j配置-->
  <!--<context-param>-->
  <!--<param-name>log4jRefreshInterval</param-name>-->
  <!--<param-value>60000</param-value>-->
  <!--</context-param>-->
  <!--logback日志配置-->
  <context-param>
    <param-name>logbackConfigLocation</param-name>
    <param-value>classpath:logback.xml</param-value>
  </context-param>
  <listener>
    <listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class>
  </listener>

  <!--配置ContextLoaderListener初始化IOC容器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- 防止Spring内存溢出监听器 -->
  <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  </listener>


  <!-- Spring mvc配置 -->
  <!-- ================================================= -->
  <!--DispatcherServlet前置控制器配置-->
  <servlet>
    <!--springmvc框架默认自动找到/WEB-INF/springmvc-servlet.xml作为配置文件载入web工程中 这里手动设置位置-->
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!--拦截内容:servlet映射设置-->
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!--解决中文乱码问题的过滤器-->
  <filter>
    <filter-name>characterEncodingFilter</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>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>



</web-app>

4.配置spring-mvc.xml (整合spring和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: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-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

    <!--配置springmvc注解扫描-->
    <context:component-scan base-package="com.yiyezhiqiu.controller"/>
    <!--配置不处理静态资源-->
    <mvc:default-servlet-handler/>
    <!--配置注解驱动-->
    <mvc:annotation-driven />
        <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          id="internalResourceViewResolver">
        <!--相当于寻找/jsp/xxx.jsp文件-->
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/pages/" />
        <!-- 后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>

    <!--过滤静态资源-->
    <mvc:resources location="/css/" mapping="/css/**" />
    <mvc:resources location="/imagess/" mapping="/images/**" />
    <mvc:resources location="/js/" mapping="/js/**" />

    <!--这段一用就报404,懵逼-->
    <mvc:annotation-driven >
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter" >
                <property name = "supportedMediaTypes">
                <list>
                    <value>application/json;charset=utf-8</value>
                    <value>text/html;charset=utf-8</value>
                    <!-- application 可以在任意 form 表单里面 enctype 属性默认找到 -->
                    <value>application/x-www-form-urlencoded</value>
                </list>
            </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
</beans>

5.配置spring-mybatis(spring和mybatis整合)

5.1 、写一个jdbc.properties文件存放和mysql数据库连接的字符串信息

mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=UTF8
mysql.username=root
mysql.password=root

initPoolSize=3
maxPoolSizes=30

5.2、配置spirng和mybatis

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc.xsd
                        http://www.springframework.org/schema/cache
                        http://www.springframework.org/schema/cache/spring-cache.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- http://www.springframework.org/schema/mvc:这种就是spring框架的约束-->
    <!--使用注解开发-->
    <mvc:annotation-driven/>
    <context:component-scan base-package="com.yiyezhiqiu"/>
    <!--引入jdbc.properties-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--配置数据源:C3P0,主要为了和数据库进行连接-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${mysql.driver}"/>
        <property name="jdbcUrl" value="${mysql.url}"/>
        <property name="user" value="${mysql.username}"/>
        <property name="password" value="${mysql.password}"/>

        <property name="initialPoolSize" value="${initPoolSize}"/>
        <property name="maxPoolSize" value="${maxPoolSizes}"/>
    </bean>

    <!--配置SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!--引入数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--加载所有的映射文件-->
        <property name="mapperLocations" value="classpath:com.yiyezhiqiu.dao/*.xml"/>
        <!--tyepAliasesPackage 对应实体类的包,这个时候会自动取包名作为别名-->
        <property name="typeAliasesPackage" value="com.yiyezhiqiu.domain"/>
        <property name="configuration">
            <!--可以将之前mybatis.cfg.xml的一些配置项转移到这里来-->
            <bean class="org.apache.ibatis.session.Configuration">
                <property name="mapUnderscoreToCamelCase" value="true" />
            </bean>
        </property>

    </bean>

    <!--自动扫描对象关系映射MapperScannerConfigurer-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <property name="basePackage" value="com.yiyezhiqiu.dao"/>
    </bean>

    <!-- 数据库事务管理 -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

</beans>

6.创建代码和表

6.1、Users

package com.yiyezhiqiu.domain;

import java.io.Serializable;

public class Users implements Serializable {
    private Integer id;
    private Integer uid;
    private String userName;
    private String password;


    public Users(Integer id, Integer uid, String userName, String password) {
        this.id = id;
        this.uid = uid;
        this.userName = userName;
        this.password = password;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


    @Override
    public String toString() {
        return "Users{" +
                "id=" + id +
                ", uid=" + uid +
                ", userName=‘" + userName + ‘\‘‘ +
                ", password=‘" + password + ‘\‘‘ +
                ‘}‘;
    }
}

6.2、UsersController

@RestController
@RequestMapping(value = "/users")
public class UsersController {

    @Autowired
    IUsersService usersService;

    @RequestMapping(value = "/saveUsers",method = RequestMethod.GET)
    @ResponseBody
    public String saveUsers(Users users){
        System.out.println("users:"+users);
         int result = usersService.saveUsers(users);
         if(result>0){
             return"保存成功";
         }else{
             return"保存失败";
         }
    }
}

6.3、UsersDao

@Repository
public interface UsersDao {

    int saveUsers(Users users);

}

6.4、UsersDao.xml

<?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" >
<mapper namespace="com.yiyezhiqiu.dao.UsersDao">
<insert id="saveUsers" parameterType="com.yiyezhiqiu.domain.Users" keyProperty="id" useGeneratedKeys="true">
    insert into  users(uid,userName,password) values (#{uid},#{userName},#{password})
</insert>
</mapper>

6.5、mysql表创建相应表和字段即可。

6.6、测试访问即可。

 

idea+SSM+Mysql框架整合

上一篇:MySQL管理工具-SQLyog 9.63的使用详解


下一篇:MySQL安装