嘀嘀嘀~~~  页面这在飞快的跑来 . . .

Idea Maven SSM整合配置


准备工作

IntelliJ IDEA

maven环境

JDK环境

项目结构

│  pom.xml
│  
├─src
│  ├─main
│  │  ├─java
│  │  │  └─vip
│  │  │      └─wulinzeng
│  │  │          ├─controller
│  │  │          │      CustomerController.java
│  │  │          │      
│  │  │          ├─mapper
│  │  │          │      CustomerMapper.java
│  │  │          │      
│  │  │          ├─pojo
│  │  │          │      Customer.java
│  │  │          │      
│  │  │          └─service
│  │  │              │  CustomerService.java
│  │  │              │  
│  │  │              └─impl
│  │  │                      CustomerServiceImpl.java
│  │  │                      
│  │  ├─resources
│  │  │  │  applicationContext.xml
│  │  │  │  jdbc.properties
│  │  │  │  log4j.properties
│  │  │  │  spring-mvc.xml
│  │  │  │  sqlMapConfig-spring.xml
│  │  │  │  sqlMapConfig.xml
│  │  │  │  
│  │  │  └─vip
│  │  │      └─wulinzeng
│  │  │          └─mapper
│  │  │                  CustomerMapper.xml
│  │  │                  
│  │  └─webapp
│  │      │  index.jsp
│  │      │  
│  │      └─WEB-INF
│  │          │  web.xml
│  │          │  
│  │          └─views
│  │                  customer_list.jsp
│  │                  
│  └─test
│      └─java
│              Test.java
│              
└─target
    ├─classes
    │  │  applicationContext.xml
    │  │  jdbc.properties
    │  │  log4j.properties
    │  │  spring-mvc.xml
    │  │  sqlMapConfig-spring.xml
    │  │  sqlMapConfig.xml
    │  │  
    │  └─vip
    │      └─wulinzeng
    │          ├─controller
    │          │      CustomerController.class
    │          │      
    │          ├─mapper
    │          │      CustomerMapper.class
    │          │      CustomerMapper.xml
    │          │      
    │          ├─pojo
    │          │      Customer.class
    │          │      
    │          └─service
    │              │  CustomerService.class
    │              │  
    │              └─impl
    │                      CustomerServiceImpl.class
    │                      
    ├─generated-sources
    │  └─annotations
    ├─generated-test-sources
    │  └─test-annotations
    ├─MybatisDemoTwo
    │  │  index.jsp
    │  │  
    │  ├─META-INF
    │  │      MANIFEST.MF
    │  │      
    │  └─WEB-INF
    │      │  web.xml
    │      │  
    │      ├─classes
    │      │  │  applicationContext.xml
    │      │  │  jdbc.properties
    │      │  │  log4j.properties
    │      │  │  spring-mvc.xml
    │      │  │  sqlMapConfig-spring.xml
    │      │  │  sqlMapConfig.xml
    │      │  │  
    │      │  ├─mapper
    │      │  ├─vip
    │      │  │  └─wulinzeng
    │      │  │      ├─controller
    │      │  │      │      CustomerController.class
    │      │  │      │      
    │      │  │      ├─mapper
    │      │  │      │      CustomerMapper.class
    │      │  │      │      CustomerMapper.xml
    │      │  │      │      
    │      │  │      ├─pojo
    │      │  │      │      Customer.class
    │      │  │      │      
    │      │  │      └─service
    │      │  │          │  CustomerService.class
    │      │  │          │  
    │      │  │          └─impl
    │      │  │                  CustomerServiceImpl.class
    │      │  │                  
    │      │  └─vip.wulinzeng
    │      │      └─mapper
    │      ├─lib
    │      │      aspectjweaver-1.8.7.jar
    │      │      c3p0-0.9.1.2.jar
    │      │      hamcrest-core-1.3.jar
    │      │      jsp-api-2.0.jar
    │      │      jstl-1.2.jar
    │      │      junit-4.12.jar
    │      │      log4j-1.2.17.jar
    │      │      mybatis-3.4.5.jar
    │      │      mybatis-spring-1.3.1.jar
    │      │      MybatisDemo01-1.0-SNAPSHOT.jar
    │      │      mysql-connector-java-5.1.6.jar
    │      │      servlet-api-2.5.jar
    │      │      spring-aop-5.0.5.RELEASE.jar
    │      │      spring-beans-5.0.5.RELEASE.jar
    │      │      spring-context-5.0.5.RELEASE.jar
    │      │      spring-core-5.0.5.RELEASE.jar
    │      │      spring-expression-5.0.5.RELEASE.jar
    │      │      spring-jcl-5.0.5.RELEASE.jar
    │      │      spring-jdbc-5.0.5.RELEASE.jar
    │      │      spring-test-5.0.5.RELEASE.jar
    │      │      spring-tx-5.0.5.RELEASE.jar
    │      │      spring-web-5.0.5.RELEASE.jar
    │      │      spring-webmvc-5.0.5.RELEASE.jar
    │      │      
    │      └─views
    │              customer_list.jsp
    │              
    └─test-classes
            Test.class

整合

resources

pom.xml

    <!--spring相关-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.7</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>

    <!--servlet和jsp-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
    </dependency>

    <!--mybatis相关-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.5</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.1</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.6</version>
    </dependency>
    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
      <dependency>
          <groupId>org.example</groupId>
          <artifactId>MybatisDemo01</artifactId>
          <version>1.0-SNAPSHOT</version>
          <scope>compile</scope>
      </dependency>

加载配置文件在build中添加resources

  <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
      </resource>
    </resources>

applicationContext.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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/context
http://www.springframework.org/schema/context/spring-context.xsd">

    <!--组件扫描 扫描service和mapper-->
    <context:component-scan base-package="vip.wulinzeng">
        <!--排除controller的扫描-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
    </context:component-scan>

    <!--加载propeties文件-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

    <!--配置数据源信息-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--配置sessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!--加载mybatis核心文件-->
        <property name="configLocation" value="classpath:sqlMapConfig-spring.xml"></property>
    </bean>

    <!--扫描mapper所在的包 为mapper创建实现类-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="vip.wulinzeng.mapper"></property>
    </bean>


    <!--声明式事务控制-->
    <!--平台事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--配置事务增强-->
    <tx:advice id="txAdvice">
        <tx:attributes>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <!--事务的aop织入-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* vip.wulinzeng.service.impl.*.*(..))"></aop:advisor>
    </aop:config>

</beans>

jdbc.properties

修改数据库表名,数据库用户名,密码。8.0以上需设置时区serverTimezone=UTC

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_test?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=

log4j.properties

#
# Hibernate, Relational Persistence for Idiomatic Java
#
# License: GNU Lesser General Public License (LGPL), version 2.1 or later.
# See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
#

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=all, stdout

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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="vip.wulinzeng.controller"></context:component-scan>
    <!--配置mvc注解驱动-->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!--内部资源视图解析器-->
    <bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!--开发静态资源访问权限-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
</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>

    <!--加载properties文件-->
    <properties resource="jdbc.properties"></properties>

    <!--定义别名-->
    <typeAliases>
        <!--<typeAlias type="com.itheima.domain.Account" alias="account"></typeAlias>-->
        <package name="vip.wulinzeng.controller"></package>
    </typeAliases>

    <!--环境-->
    <environments default="developement">
        <environment id="developement">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" 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>
            </dataSource>
        </environment>
    </environments>

    <!--加载映射-->
    <mappers>
        <!--<mapper resource="com/itheima/mapper/AccountMapper.xml"></mapper>-->
        <package name="vip.wulinzeng.mapper"></package>
    </mappers>

</configuration>

sqlMapConfig-spring.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>

    <!--定义别名-->
    <typeAliases>
        <!--<typeAlias type="com.itheima.domain.Account" alias="account"></typeAlias>-->
        <package name="vip.wulinzeng.controller"></package>
    </typeAliases>

</configuration>

CustomerMapper.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="vip.wulinzeng.mapper.CustomerMapper">

    <select id="findAll"  resultType="vip.wulinzeng.pojo.Customer">
         select * from customer
     </select>

</mapper>

java

CustomerController

package vip.wulinzeng.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import vip.wulinzeng.pojo.Customer;
import vip.wulinzeng.service.CustomerService;

import java.util.List;

@Controller
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @RequestMapping("/findAll")
    public ModelAndView findAll(ModelAndView model){
        System.out.println("findAll get in");
        List<Customer> customerList=customerService.findAll();
        System.out.println(customerList);
        model.addObject("customer",customerList);
        model.setViewName("customer_list");
        return  model;
    }
}

CustomerMapper

package vip.wulinzeng.mapper;

import vip.wulinzeng.pojo.Customer;

import java.util.List;

public interface CustomerMapper {
    public List<Customer> findAll();
}

Customer

set get toString方法自行补齐,数据库表结构参考实体类,id自增。

package vip.wulinzeng.pojo;

public class Customer {
    private Integer id;
    private String username;
    private String jobs;
    private String phone;

   /* set get tostring */
}

CustomerServiceImpl

package vip.wulinzeng.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.wulinzeng.mapper.CustomerMapper;
import vip.wulinzeng.pojo.Customer;
import vip.wulinzeng.service.CustomerService;

import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public List<Customer> findAll() {
        return customerMapper.findAll();
    }
}

CustomerService

package vip.wulinzeng.service;

import vip.wulinzeng.pojo.Customer;

import java.util.List;

public interface CustomerService {

    public List<Customer> findAll();
}

webapp

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

  <!--spring 监听器-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!--springmvc的前端控制器-->
  <servlet>
    <servlet-name>DispatcherServlet</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-mapping>
    <servlet-name>DispatcherServlet</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>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

</web-app>

customer_list.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>展示账户数据列表</h1>
<table border="1">
    <tr>
        <th>账户id</th>
        <th>账户名称</th>
        <th>账户职位</th>
        <th>账户电话</th>
    </tr>
    <c:forEach items="${customer}" var="customer">
        <tr>
            <td>${customer.id}</td>
            <td>${customer.username}</td>
            <td>${customer.jobs}</td>
            <td>${customer.phone}</td>
        </tr>
    </c:forEach>
</table>
</body>
</html>

index.jsp

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

项目地址

项目下载:https://github.com/unluckynike/BaseSSM


文章作者: HaiLin Zhou
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 HaiLin Zhou !
评论
 上一篇
指令系统 指令系统
指令系统是计算机硬件的语言系统,也叫机器语言,它是软件和硬件的主要界面,从系统结构的角度看,它是系统程序员看到的计算机的主要属性。指令系统是表征一台计算机性能的重要因素,它的格式与功能不仅直接影响到机器的硬件结构,而且也直接影响到系统软件
下一篇 
阿里云ECS项目部署 阿里云ECS项目部署
阿里云服务器部署SSM项目
2020-10-27
  目录