新建父项目
youfanmiaosha2
pom文件
共有的依赖spring boot,spring boot有父依赖
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.0modelVersion> <groupId>com.youfan.miaosha2groupId> <artifactId>youfanmiaosha2artifactId> <packaging>pompackaging> <version>1.0version> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.0.2.RELEASEversion> parent> <properties> <spring-cloud.version>Finchley.RELEASEspring-cloud.version> properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-dependenciesartifactId> <version>${spring-cloud.version}version> <type>pomtype> <scope>importscope> dependency> dependencies> dependencyManagement> project>
参考:dependencyManagement与dependencies区别
最近在阅读maven项目代码时,dependencyManagement与dependencies之间的区别不是很了解,现通过项目实例进行总结:项目epps-demob-pom下有一个模块是epps-demob-war。
一、dependencyManagement应用场景
为了项目的正确运行,必须让所有的子模块使用依赖项的统一版本,必须确保应用的各个项目的依赖项和版本一致,才能保证测试的和发布的是相同的结果。在我们项目顶层的pom文件中,我们会看到dependencyManagement元素。通过它元素来管理jar包的版本,让子项目中引用一个依赖而不用显示的列出版本号。Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用在这个dependencyManagement元素中指定的版本号。
epps-demob-pom中dependencyManagement如下:
epps-demob-war 3.1.1.RELEASE org.springframework spring-web ${spring-version} epps-demob-war中dependency如下:
org.springframework spring-web 这样做的好处:统一管理项目的版本号,确保应用的各个项目的依赖和版本一致,才能保证测试的和发布的是相同的成果,因此,在顶层pom中定义共同的依赖关系。同时可以避免在每个使用的子项目中都声明一个版本号,这样想升级或者切换到另一个版本时,只需要在父类容器里更新,不需要任何一个子项目的修改;如果某个子项目需要另外一个版本号时,只需要在dependencies中声明一个版本号即可。子类就会使用子类声明的版本号,不继承于父类版本号。
二、dependencies应用场景
相对于dependencyManagement,如果在epps-demob-pom中通过dependencies引入jar,将默认被所有的子模块继承。
三、dependencyManagement与dependencies区别
dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显式的声明需要用的依赖。如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。
dependencies即使在子模块中不写该依赖项,那么子模块仍然会从父项目中继承该依赖项(全部继承)。
新建模块:注册中心
pom引入注册中心的依赖
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>youfanmiaosha2artifactId> <groupId>com.youfan.miaosha2groupId> <version>1.0version> parent> <modelVersion>4.0.0modelVersion> <artifactId>youfanRegisterCenterartifactId> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId> dependency> dependencies> <build> <finalName>youfanRegisterCenterfinalName> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> <executions> <execution> <goals> <goal>repackagegoal> goals> execution> executions> plugin> plugins> <resources> <resource> <directory>src/main/resourcesdirectory> <targetPath>${project.build.directory}/classestargetPath> <includes> <include>**/*include> includes> <filtering>truefiltering> resource> resources> build> project>
resources文件夹下配置文件application.yml
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false # 是否向 Eureka 注册服务。该应用为服务注册中心,不需要自注册,设置为 false
fetchRegistry: false # 是否检索服务。该应用为服务注册中心,职责为注册和发现服务,无需检索服务,设置为 false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: youfanRegisterCenter
新建包com.youfan.controller
包下新建类eureka
package com.youfan.controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaServer { public static void main(String[] args) { SpringApplication.run(EurekaServer.class,args); } }
用户模块设计
新建模块youfanUser
pom添加依赖
作为客户端,引入eureka
下面用到mybatis,引入
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>youfanmiaosha2artifactId> <groupId>com.youfan.miaosha2groupId> <version>1.0version> parent> <modelVersion>4.0.0modelVersion> <artifactId>youfanUserartifactId> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starterartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> dependencies> build>
resources文件夹下创建application.properties
server.port=8081
spring.application.name=youfanUser
新建com.youfan包
包下建YouFanUserApplication类
package com.youfan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication public class YouFanUserApplication { public static void main(String[] args) { SpringApplication.run(YouFanUserApplication.class, args); } }
建control包
建测试类UserTest
package com.youfan.control; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; @RestController public class UserTest { @RequestMapping(value="sayHello", method= RequestMethod.GET) public String sayHello(HttpServletRequest req){ String remoteAddr = req.getRemoteAddr(); return "hello " + remoteAddr + "!!"; } }
启动
建service包
新建数据库youfanshop
新建表youfanuser
新建模块youfanCommon
pom配置常用工具
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>youfanmiaosha2artifactId> <groupId>com.youfan.miaosha2groupId> <version>1.0version> parent> <modelVersion>4.0.0modelVersion> <artifactId>youfanCommonartifactId> <dependencies> <dependency> <groupId>com.alibabagroupId> <artifactId>fastjsonartifactId> <version>1.2.54version> dependency> <dependency> <groupId>org.apache.commonsgroupId> <artifactId>commons-lang3artifactId> <version>3.3.2version> dependency> <dependency> <groupId>commons-codecgroupId> <artifactId>commons-codecartifactId> <version>1.11version> dependency> <dependency> <groupId>redis.clientsgroupId> <artifactId>jedisartifactId> <version>2.9.0version> dependency> dependencies> project>
新建包com.youfan.entity
新建类UserInfo
package com.youfan.entity; public class UserInfo { private int id;//主键 private String account;//用户名 private String encryptionPassword;//加密密码 private String originalPassword;//未加密密码 private int sex;//性别 0男1女 private String telphone;//手机号 private String birthday;//生日 private String qq;//qq private String weixin;//微信号 private String idCard;//身份证号 private String name;//姓名 private int age;//年龄 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getEncryptionPassword() { return encryptionPassword; } public void setEncryptionPassword(String encryptionPassword) { this.encryptionPassword = encryptionPassword; } public String getOriginalPassword() { return originalPassword; } public void setOriginalPassword(String originalPassword) { this.originalPassword = originalPassword; } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public String getTelphone() { return telphone; } public void setTelphone(String telphone) { this.telphone = telphone; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getQq() { return qq; } public void setQq(String qq) { this.qq = qq; } public String getWeixin() { return weixin; } public void setWeixin(String weixin) { this.weixin = weixin; } public String getIdCard() { return idCard; } public void setIdCard(String idCard) { this.idCard = idCard; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
整合mybatis
在youfanUser的pom中添加依赖
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>youfanmiaosha2artifactId> <groupId>com.youfan.miaosha2groupId> <version>1.0version> parent> <modelVersion>4.0.0modelVersion> <artifactId>youfanUserartifactId> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starterartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-jdbcartifactId> dependency> <dependency> <groupId>org.mybatis.spring.bootgroupId> <artifactId>mybatis-spring-boot-starterartifactId> <version>1.3.1version> dependency> <dependency> <groupId>org.apache.tomcatgroupId> <artifactId>tomcat-jdbcartifactId> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-thymeleafartifactId> dependency> dependencies> <build> <finalName>youfanUserfinalName> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> <executions> <execution> <goals> <goal>repackagegoal> goals> execution> executions> plugin> plugins> <resources> <resource> <directory>src/main/resourcesdirectory> <targetPath>${project.build.directory}/classestargetPath> <includes> <include>**/*include> includes> <filtering>truefiltering> resource> resources> build> project>
YouFanUserApplication中添加bean和注解
package com.youfan; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.tomcat.jdbc.pool.DataSource; import org.mybatis.spring.SqlSessionFactoryBean; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; @SpringBootApplication @EnableEurekaClient
@MappScan("com.youfan.mapper") public class YouFanUserApplication { @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource(){ return new org.apache.tomcat.jdbc.pool.DataSource(); } @Bean public SqlSessionFactory sqlSessionFactoryBean() throws Exception{ SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource()); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mappers/*.xml")); return sqlSessionFactoryBean.getObject(); } @Bean public PlatformTransactionManager transactionManager() { return new DataSourceTransactionManager(dataSource()); } public static void main(String[] args) { SpringApplication.run(YouFanUserApplication.class, args); } }
pom添加youfanCommon依赖
<dependency> <groupId>com.youfan.miaosha2groupId> <version>1.0version> <artifactId>youfanCommonartifactId> dependency>
新建com.youfan.mapper包,下面新建UserMapper接口
package com.youfan.mapper; import com.youfan.entity.UserInfo; public interface UserMapper { public UserInfo findUserByid(int userid); }
新建上面classpath路径和文件,resources下新建User.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.youfan.mapper.UserMapper"> <select id="findUserByid" resultType="com.youfan.entity.UserInfo" parameterType="int"> select account,encryptionPassword,sex,telphone,qq,weixin,age,birthday from youfanuser where id = #{userid} select> mapper>
application.properties中配置数据库
server.port=8081 spring.application.name=youfanUser eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ #mysql配置 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/youfanshop?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver
新建com.youfan.dao包
新建UserDao
package com.youfan.dao; import com.youfan.entity.UserInfo; import com.youfan.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class UserDao { @Autowired private UserMapper userMapper; public UserInfo findUserByid(int userid){ return userMapper.findUserByid(userid); } }
idea报Could not autowired解决办法
service下新建UserService
package com.youfan.service; import com.youfan.dao.UserDao; import com.youfan.entity.UserInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { @Autowired private UserDao userDao; public UserInfo findUserByid(int userid){ return userDao.findUserByid(userid); } }
control下新建UserControl
package com.youfan.control; import com.alibaba.fastjson.JSONObject; import com.youfan.entity.UserInfo; import com.youfan.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class UserControl { @Autowired private UserService userService; @RequestMapping(value = "findUserbyId", method = RequestMethod.GET) public String findUserbyId(int id){ UserInfo userInfo = userService.findUserByid(id); String result = JSONObject.toJSONString(userInfo); return result; } }
配置maven仓库镜像,settings.xml中添加:
alimaven
aliyun maven
http://maven.aliyun.com/nexus/content/repositories/central/
central
junit
junit Address/
http://jcenter.bintray.com/
central
数据库插入一条数据
启动
整合thymeleaf
pom依赖
<dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-thymeleafartifactId> dependency>
resources下新建文件夹templates
新建viewUser.html
DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Titletitle>
head>
<body>
<table border="1">
<tr>
<td>账号td>
<td>手机号td>
<td>生日td>
<td>qqtd>
<td>微信td>
tr>
<tr>
<td><span th:text="${userInfo.account}"/>td>
<td><span th:text="${userInfo.telphone}"/>td>
<td><span th:text="${userInfo.birthday}"/>td>
<td><span th:text="${userInfo.qq}"/>td>
<td><span th:text="${userInfo.weixin}"/>td>
tr>
table>
body>
html>
修改UserControl
package com.youfan.control; import com.alibaba.fastjson.JSONObject; import com.youfan.entity.UserInfo; import com.youfan.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.Controller; @Controller public class UserControl { @Autowired private UserService userService; @RequestMapping(value = "findUserbyId", method = RequestMethod.GET) public String findUserbyId(int id, Model model){ UserInfo userInfo = userService.findUserByid(id); model.addAttribute("userInfo", userInfo); return "viewUser"; } }
报错org.thymeleaf.exceptions.TemplateInputException: Error resolving template解决方案