maven to build a layered architecture

1. Preparation:

  

1 . Creating a data source
CREATE TABLE `users` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `price` float(10,0) DEFAULT NULL,
  `pic` varchar(40) DEFAULT NULL,
  `createtime` datetime DEFAULT NULL,
  `detail` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8;

2 . Project modules are as follows:
the Users - parent POM parent project, the unified management of sub-projects and jar package version
Users - Model JAR package the JavaBean
the Users - DAO persistence layer JAR
the Users - Service JAR business layer
Users -web control layer WAR

 

2. Create the parent project:

  (1) introducing 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.bky</groupId>
<artifactId>users-parent</artifactId>
<version>1.0-SNAPSHOT</version>

<-! Management modules -> you can create your own modules created after the
<modules>
<Module> the Users-Model </ Module>
<Module> the Users-DAO </ Module>
<Module> the Users-Service </ Module>
< Module1> Web-Users </ Module1>
</ modules>
<Packaging> POM </ Packaging>

<-!
1.jar package management
2. Management sub-projects
->

<properties>
<spring.version>5.0.2.RELEASE</spring.version>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
<mysql.version>5.1.6</mysql.version>
<mybatis.version>3.4.5</mybatis.version>
<aspectjweaver.version>1.6.8</aspectjweaver.version>
<junit.version>4.12</junit.version>
<jsp-api.version>2.0</jsp-api.version>
<servlet-api.version>2.5</servlet-api.version>
<jstl.version>1.2</jstl.version>
<mybatis-spring.version>1.3.0</mybatis-spring.version>
<druid.version>1.0.9</druid.version>
<!--文件的编码格式-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<-!
Jar package management
dependencyManagement: manages only coordinate information jar package, and not rely to the project
dependencies: jar package dependencies (jar package will be imported to come)
->

<!--jar包管理-->
<dependencyManagement>
<!--引入依赖-->
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectjweaver.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>

<!--spring包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>

<!--用于SpringMVC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>

<!--用于数据库源相关操作-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>

<!--ServletAPI-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet-api.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp-api.version}</version>
<scope>provided</scope>
</dependency>

<!--jstl标签-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>

<!--MySQL数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>

<!--测试框架-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>compile</scope>
</dependency>


<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->

<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>

<!--MyBatis集成Spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>

<!--数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

 


</project>

 

3. Create users-model module

  (1) introducing 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">
<!--指定父工程-->
<parent>
<artifactId>users-parent</artifactId>
<groupId>com.bky</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>users-model</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

</project>

 

  (2) create javabean

 

4. Create users-dao module

  (1) introducing 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">
<parent>
<artifactId>users-parent</artifactId>
<groupId>com.bky</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>users-dao</artifactId>
<packaging>jar</packaging>
<version>1.0-RELEASES</version>

<!--引入依赖-->
<dependencies>
<!--model的依赖-->
<dependency>
<groupId>com.bky</groupId>
<artifactId>users-model</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>

<!--MyBatis集成Spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>

<!--数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>

<!--MySQL数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

<!--SpringJdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>

<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<!-- log end -->
</dependencies>

 

 

  (2) usersMapper.xml mapper

<?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.bky.mapper.usersMapper">

    <! - according to the query ID ->
    <select id="getById" parameterType="int" resultType="users">
      select * from users where id=#{id}
    </select>
</mapper>

  (3) create an interface dao

  

public  interface UsersMapper {
    users getById(Integer id);
}

  (4) create a spring-dao.xml profile (forget to write, and only put back up)

  

<?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"
       xs    i:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">


    <-! Database Connection Pool ->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          destroy-method="close">
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/maven?characterEncoding=utf8" />
        <property name="username" value="root" />
        <property name="password" value="itcast" />
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    </bean>

    <!-- SqlSessionFactoryBean -->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="typeAliasesPackage" value="com.bky.model" />
        <property name="dataSource" ref="dataSource" />
    </bean>

    <! - Configure Interface scan package ->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
              p:basePackage="com.bky.mapper"/>
</beans>

  typeAliasesPackage: Alias ​​the package path specified JavaBean

5. Create users-service

  (1) introducing 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">
<parent>
<artifactId>users-parent</artifactId>
<groupId>com.bky</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>users-service</artifactId>
<packaging>jar</packaging>

<!--引入依赖-->
<dependencies>
<!--依赖dao-->
<dependency>
<groupId>com.bky</groupId>
<artifactId>users-dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

<!-- spring -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
</dependencies>

</project>

 

  (2) create a spring-service.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"
xmlns:aop="http://www.springframework.org/schema/aop"
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://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">


<! -
1. Pack scan
Configuration statement transaction
a ready source of data.
B configuration transaction manager.
C configurable transactional execution rules [transaction propagation characteristics].
D configuration notification, specified cut-off point.
->

<!--包扫描-->
<context:component-scan base-package="com.bky">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<! -
Configure declarative transaction
ready data source
<import resource = "spring-mybatis.xml " />: corresponds to the configuration of the spring-mybatis.xml integration over the
configuration information can be used to import configuration files directly
- >
<Import Resource = "Spring-mybatis.xml" />

<! - Configure the transaction manager ->
<the bean ID = "txManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
<! - injection data source ->
<Property name = "the dataSource" REF = "the dataSource" />
</ the bean>

<!--配置事务执行规则[事务的传播特性]-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add*" />
<tx:method name="update*" />
<tx:method name="delete*" />
<!--只读-->
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>

<!--配置通知,指定切点-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.bky.service.impl.*.*(..))" />
</aop:config>
</beans>

 Here <import resource="spring-dao.xml" />: redistributes spring-dao.xml configuration, equivalent to two configurations incorporated together.

 If springmvc the introduction of this profile, you may not need to create a spring web.xml listener with container

 

  (3) create service interfaces

  

public interface usersService {
    users getById(Integer id);
}

  (4) to create an implementation class

  

@Service
public class usersServiceImpl implements usersService {

    @Autowired
    private usersMapper usersMapper;

    @Override
    public users getById(Integer id) {
        return usersMapper.getById(id);
    }
}

 

6. Create users-web module

  (1) introducing 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">
<parent>
<artifactId>users-parent</artifactId>
<groupId>com.bky</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>users-web</artifactId>
<packaging>war</packaging>

<dependencies>
<!--依赖service-->
<dependency>
<groupId>com.bky</groupId>
<artifactId>users-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

<!--SpringMVC依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>

<!--servletAPI -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>


<Build>
<-! plugins ->
<plugins>
<-!
Tomcat plug-ins: Items must be installed to run a local warehouse
->
<plugin>
<groupId> org.apache.tomcat.maven </ groupId>
< the artifactId> tomcat7-Maven-plugin </ the artifactId>
<Version> 2.2 </ Version>
<-! widget configuration used ->
<configuration>
<-! port number ->
<port> 18081 </ port >
<! - write the name of the current project (virtual path), if the write /, then each time you visit the project do not need to add the name of the project ->
<path> / </ path>
<! - solve get garbled request ->
<URIEncoding> UTF-. 8 </ URIEncoding>
</ Configuration>
</ plugin>
</ plugins>
</ Build>
</ Project>

 

  (2)web.xml

The works are a web project, the front end of the core needs to be configured SpringMVC controller and encoder filters web.xml POST code is as follows:

 
 

<?xml version="1.0" encoding="UTF-8"?>
<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_3_1.xsd"
version="3.1">

 
 

<-!
SpringMVC: configure the core front-end controller
configured POST request encoding filter
configuration SpringMVC core profile
->

 
 


<!--SpringMVC:配置核心前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

 
 

<! - Specifies the SpringMVC core configuration file ->
<the init-param>
<param-name> contextConfigLocation </ param-name>
<param-value> the CLASSPATH: springmvc.xml </ param-value>
</ the init-param >
<! - start load priority [Create container SpringMVCIOC] ->
<load-ON-startup>. 1 </ load-ON-startup>
</ the servlet>

<-Mapping the servlet>
<the servlet-name> DispatcherServlet </ name-the servlet>
<URL-pattern> / </ URL-pattern>
</ Mapping the servlet->

 
 


<!--配置编码过滤器POST请求-->
<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>

 
 


<!--指定Spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>

 
 

<!--监听器,创建SpringIOC容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

 

 

  (3) create springmvc.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: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.bky" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<! - annotation-driven ->
<MVC: Annotation-Driven />

<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/pages/" />
<!--后缀-->
<property name="suffix" value=".jsp" />
</bean>

<-! Static resource filtering configuration ->
<MVC: Resources Mapping = "/ ImagesRF Royalty Free / **" LOCATION = "/ ImagesRF Royalty Free /" />
</ Beans>

 

  (4) Creating Controller (web layer interface)

@Controller
@RequestMapping(value = "/users")
public class usersController {

    @Autowired
    private usersService usersService;

    /***
     * According to the query ID
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/one")
    public String getById(Integer id, Model model){
        users users = usersService.getById(id);
        model.addAttribute("users",users);
        return "users";
    }
}

 

Guess you like

Origin www.cnblogs.com/d1320/p/11012671.html