目录
6.业务实现和mapper层实现(maven-service)
7.web工程(maven-web) 即controller层
分布式架构或微服务架构具有继承关系的工程。优点:可以统一对依赖进行管理
packaging必须是pom,管理依赖版本及子工程,不需要真实的依赖的引用
一般使用depengdecyManagement管理
一般分布式架构是分两个情况进行拆分 ---1.按照功能拆分 2.按照性能拆分
主要设置一些共同要用的的依赖的版本设置
- <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>com.sofwingroupId>
- <artifactId>maven-ssmartifactId>
- <packaging>pompackaging>
- <version>1.0-SNAPSHOTversion>
- <modules>
- <module>maven-entitymodule>
- <module>maven-common1module>
- <module>maven-generatormodule>
- <module>maven-service-apimodule>
- <module>maven-servicemodule>
- <module>maven-webmodule>
- modules>
- <properties>
- <spring-version>5.3.19spring-version>
- <mybaits-version>3.5.9mybaits-version>
- <druid-version>1.2.8druid-version>
- <mysql-version>8.0.28mysql-version>
- <mybatis-spring-version>2.0.7mybatis-spring-version>
- <mybatis-generator-version>1.3.7mybatis-generator-version>
- <pageHelper-version>5.2.0pageHelper-version>
- properties>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>${spring-version}version>
- dependency>
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>${mybaits-version}version>
- dependency>
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druidartifactId>
- <version>${druid-version}version>
- dependency>
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>${mysql-version}version>
- dependency>
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatis-springartifactId>
- <version>${mybatis-spring-version}version>
- dependency>
- <dependency>
- <groupId>org.mybatis.generatorgroupId>
- <artifactId>mybatis-generator-coreartifactId>
- <version>${mybatis-generator-version}version>
- dependency>
- <dependency>
- <groupId>com.github.pagehelpergroupId>
- <artifactId>pagehelperartifactId>
- <version>${pageHelper-version}version>
- dependency>
- dependencies>
- dependencyManagement>
-
- project>
主要是逆向工程生产pojo和mapper
- <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>maven-ssmartifactId>
- <groupId>com.sofwingroupId>
- <version>1.0-SNAPSHOTversion>
- parent>
- <description>逆向工程description>
- <modelVersion>4.0.0modelVersion>
-
- <artifactId>maven-generatorartifactId>
- <dependencies>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- dependency>
-
- <dependency>
- <groupId>org.mybatis.generatorgroupId>
- <artifactId>mybatis-generator-coreartifactId>
- dependency>
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- dependency>
- dependencies>
-
- <build>
- <plugins>
- <plugin>
-
- <groupId>org.mybatis.generatorgroupId>
- <artifactId>mybatis-generator-maven-pluginartifactId>
- <version>1.3.7version>
-
- <dependencies>
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>8.0.28version>
- dependency>
- dependencies>
- plugin>
- plugins>
- build>
- project>
逆向工程的xml配置--必须是generatorConfig.xml的名字 并且在resources中
- generatorConfiguration PUBLIC
- "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
- "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
- <generatorConfiguration>
- <context id="simple" targetRuntime="MyBatis3">
- <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
- connectionURL="jdbc:mysql://localhost:3306/test?serverTimezone=UTC&nullCatalogMeansCurrent=true"
- userId="root"
- password="root" />
-
- <javaModelGenerator targetPackage="com.sofwin.pojo" targetProject="..\maven-entity\src\main\java"/>
-
- <sqlMapGenerator targetPackage="com.sofwin.mapper" targetProject="..\maven-service\src\main\java"/>
-
- <javaClientGenerator type="XMLMAPPER" targetPackage="com.sofwin.mapper" targetProject="..\maven-service\src\main\java"/>
-
- <table tableName="b_user" />
-
- context>
- generatorConfiguration>
工具模块主要就是一些工具类,这里我们没有就不设置了
逆向工程自动生成
- <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>maven-ssmartifactId>
- <groupId>com.sofwingroupId>
- <version>1.0-SNAPSHOTversion>
- parent>
- <description>service接口description>
- <modelVersion>4.0.0modelVersion>
-
- <artifactId>maven-service-apiartifactId>
- <dependencies>
- <dependency>
- <groupId>com.sofwingroupId>
- <artifactId>maven-entityartifactId>
- <version>1.0-SNAPSHOTversion>
- dependency>
- <dependency>
- <groupId>com.github.pagehelpergroupId>
- <artifactId>pagehelperartifactId>
- dependency>
- dependencies>
-
- project>
在接口中写一些方法
- package com.sofwin;
-
- import com.github.pagehelper.PageInfo;
- import com.sofwin.pojo.BUser;
-
- /**
- * @author : wentao
- * @version : 1.0
- */
- public interface UserService {
- /**
- * 验证登录
- * @param user
- * @return 如果返回为空 登录失败 返回不为空登录成功并返回用户的数据库信息
- */
- BUser checkLogin(BUser user);
-
-
-
- /**
- * 根据条件查询用户分页信息
- * @param user
- * @return
- */
- PageInfo selectUsers(BUser user, Integer pageNumber , Integer pageSize) ;
-
- /**
- * 根据id进行查询用户信息
- * @param id
- * @return
- */
- BUser selectById(Integer id);
-
- /**
- * 保存
- * @param user
- * @return true操作成功 false操作失败
- */
- boolean saveOrUpdate(BUser user);
-
- /**
- * 根据id来删除用户信息
- * @param id
- * @return true操作成功 false操作失败
- */
- boolean removeById(Integer id);
-
- /**
- * 批量删除
- * @param ids
- * @return true操作成功 false操作失败
- */
- boolean removeByIds(String[] ids);
-
-
-
- }
- <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>maven-ssmartifactId>
- <groupId>com.sofwingroupId>
- <version>1.0-SNAPSHOTversion>
- parent>
- <description>service实现类和dao接口description>
- <modelVersion>4.0.0modelVersion>
-
- <artifactId>maven-serviceartifactId>
-
- <dependencies>
-
- <dependency>
- <groupId>com.sofwingroupId>
- <artifactId>maven-service-apiartifactId>
- <version>1.0-SNAPSHOTversion>
- dependency>
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- dependency>
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatis-springartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-contextartifactId>
- <version>${spring-version}version>
- dependency>
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- dependency>
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druidartifactId>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-jdbcartifactId>
- <version>${spring-version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-testartifactId>
- <version>${spring-version}version>
- dependency>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.12version>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-coreartifactId>
- <version>2.17.2version>
- dependency>
- dependencies>
- <build>
- <resources>
- <resource>
- <directory>src\main\java\directory>
- <includes>
- <include>**\*.xmlinclude>
- <include>**\*.propertiesinclude>
- includes>
- resource>
- <resource>
- <directory>src\main\resources\directory>
- <includes>
- <include>**\*.xmlinclude>
- <include>**\*.propertiesinclude>
- includes>
- resource>
- resources>
- build>
- project>
resources文件夹中的配置文件
applicationContext-mybatis.xml spring的配置文件
- <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"
- 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">
- <context:component-scan base-package="com.sofwin">context:component-scan>
- <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 name="url" value="${jdbc.url}"/>
- <property name="username" value="${jdbc.username}"/>
- <property name="password" value="${jdbc.pwd}"/>
- bean>
-
- <bean id="sessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean" >
- <property name="dataSource" ref="dataSource"/>
- <property name="configLocation" value="classpath:config.xml"/>
- bean>
-
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="sqlSessionFactoryBeanName" value="sessionFactoryBean"/>
- <property name="basePackage" value="com.sofwin.mapper"/>
- bean>
- beans>
config.xml mybatis的全局配置文件
- configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
-
- <configuration>
-
- <properties resource="jdbc.properties">
-
- properties>
-
- <settings>
-
-
- <setting name="logImpl" value="LOG4J2"/>
-
-
-
- settings>
-
-
- <typeAliases>
-
-
-
- <package name="com.sofwin.pojo"/>
- typeAliases>
-
-
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor">
- <property name="helperDialect" value="mysql"/>
- plugin>
- plugins>
-
-
- <environments default="dev">
-
- <environment id="dev">
-
- <transactionManager type="JDBC">transactionManager>
-
- <dataSource type="POOLED">
-
- <property name="driver" value="${jdbc.driver}"/>
-
- <property name="url" value="${jdbc.url}"/>
- <property name="username" value="${jdbc.username}"/>
- <property name="password" value="${jdbc.pwd}"/>
- dataSource>
- environment>
- environments>
-
-
- <mappers>
-
-
- <package name="com.sofwin.mapper"/>
- mappers>
- configuration>
jdbc.properties
- jdbc.driver=com.mysql.cj.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&nullCatalogMeansCurrent=true
- jdbc.username=root
- jdbc.pwd=root
log4j2.xml
- <Configuration status="debug"
- monitorInterval="600">
- <Appenders>
- <Console name="Console"
- target="SYSTEM_OUT">
- <PatternLayout
- pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t]
- %-5level [%L] - %msg%n" />
- Console>
- Appenders>
- <Loggers>
- <Root level="DEBUG">
- <AppenderRef ref="Console" />
- Root>
- Loggers>
- Configuration>
UserServiceImpl
- package com.sofwin.service.impl;
-
- import com.github.pagehelper.PageHelper;
- import com.github.pagehelper.PageInfo;
- import com.sofwin.UserService;
- import com.sofwin.mapper.BUserMapper;
- import com.sofwin.pojo.BUser;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import java.util.List;
-
- /**
- * @author : wentao
- * @version : 1.0
- */
- @Service
- public class UserServiceImpl implements UserService {
- @Autowired
- private BUserMapper mapper;
- public BUser checkLogin(BUser user) {
- return null;
- }
-
- public PageInfo selectUsers(BUser user, Integer pageNumber, Integer pageSize) {
- PageHelper.startPage(pageNumber,pageSize);
- List
bUsers = mapper.selectByExample(null); - return new PageInfo(bUsers);
- }
-
- public BUser selectById(Integer id) {
- return null;
- }
-
- public boolean saveOrUpdate(BUser user) {
- return false;
- }
-
- public boolean removeById(Integer id) {
- return false;
- }
-
- public boolean removeByIds(String[] ids) {
- return false;
- }
- }
这里要测试一下是否成功
- package com.sofwin.test;
-
- import com.github.pagehelper.PageInfo;
- import com.sofwin.UserService;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-
- /**
- * @author : wentao
- * @version : 1.0
- */
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = "classpath:applicationContext-mybatis.xml")
- public class TestService {
- @Autowired private UserService service;
-
- @Test
- public void test01(){
- PageInfo pageInfo = service.selectUsers(null, 1, 10);
- System.out.println(pageInfo);
- }
- }
- <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>maven-ssmartifactId>
- <groupId>com.sofwingroupId>
- <version>1.0-SNAPSHOTversion>
- parent>
- <description>controller 控制层description>
- <modelVersion>4.0.0modelVersion>
-
- <artifactId>maven-webartifactId>
- <packaging>warpackaging>
- <dependencies>
- <dependency>
- <groupId>com.sofwingroupId>
- <artifactId>maven-serviceartifactId>
- <version>1.0-SNAPSHOTversion>
- dependency>
-
-
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>servlet-apiartifactId>
- <version>2.5version>
- <scope>providedscope>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.coregroupId>
- <artifactId>jackson-databindartifactId>
- <version>2.13.2version>
- dependency>
- dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.tomcat.mavengroupId>
- <artifactId>tomcat7-maven-pluginartifactId>
- <version>2.2version>
- <configuration>
-
- <path>/mavenssmpath>
- configuration>
- plugin>
- plugins>
- build>
- project>
contextConfigration-mvc.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"
- 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">
-
- <mvc:annotation-driven>mvc:annotation-driven>
-
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/"/>
- <property name="suffix" value=".jsp"/>
- bean>
- beans>
web.xml 设置前端控制器
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
-
- <servlet>
- <servlet-name>maven-webservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
- <init-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:applicationContext-mybatis.xml,
- classpath:contextConfigration-mvc.xml
- param-value>
- init-param>
- <load-on-startup>1load-on-startup>
- servlet>
- <servlet-mapping>
- <servlet-name>maven-webservlet-name>
- <url-pattern>/url-pattern>
- servlet-mapping>
- web-app>
controller
- package com.sofwin.controller;
-
- import com.github.pagehelper.PageInfo;
- import com.sofwin.UserService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
-
- /**
- * @author : wentao
- * @version : 1.0
- */
- @Controller
- @RequestMapping("/user")
- public class UserController {
- @Autowired
- private UserService service;
-
- @ResponseBody
- @GetMapping("/test01")
- public PageInfo test01(){
- PageInfo pageInfo = service.selectUsers(null, 1, 5);
- return pageInfo;
- }
-
-
- @GetMapping("/test02")
- public String test02(){
- PageInfo pageInfo = service.selectUsers(null, 1, 5);
- return "index";
- }
- }
这里我们导入自定义的依赖,现在在本地仓库还是没有的,我们要将他们全部加入到本地仓库中
这里我们直接使用父模块的声明周期的install即可全部加入到本地仓库