"In the previous article to build more advanced course module under SpringBoot (sixty) intellij idea project (on) ," we have re-introduced after differentiation in intellij idea to create a project in multiple module, and then introduce today a general introduction between each module detailed work breakdown. If the segment is not considered more module, you can read this article " SpringBoot Start Tutorial (a) Detailed intellij idea to build SpringBoot ."
v database design
CREATE TABLE `useraccount` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `username` varchar(255) NOT NULL, `age` int(10) NOT NULL, `phone` bigint NOT NULL, `email` varchar(255) NOT NULL, `account` varchar(100) NOT NULL UNIQUE, `pwd` varchar(255) NOT NULL, PRIMARY KEY (`id`) )ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; insert into `useraccount` values(1,'赵(dev)',23,158,'[email protected]','test001','test001'); insert into `useraccount` values(2,'钱(dev)',27,136,'[email protected]','test002','test002'); insert into `useraccount` values(3,'孙(dev)',31,159,'[email protected]','test003','test003'); insert into `useraccount` values(4,'李(dev)',35,130,'[email protected]','test004','test004'); select * from `useraccount`;
v introduction mybatis
1.0 plug-ins add mybatis
Add mybatis plug-in pom.xml learn-persist in.
<build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>${mybatis-generator.version}</version> <dependencies> <dependency> <groupId> mysql</groupId> <artifactId> mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>${mybatis-generator.version}</version> </dependency> </dependencies> <Configuration > <-! automatically generated configuration -> < configurationFile > the src / main / Resources / MyBatis-config / MyBatis-generator.xml </ configurationFile > <-! allow movement of the generated file -> < verbose > to true </ verbose > <-! Overwrite -> < Overwrite > to false </ Overwrite > </ Configuration > </ plugin > </ plugins > </ Build >
As shown above, created under resources learn-persist and are mapper mybatis-config file directory, and add jdbc.properties and mybatis-generator.xml file.
1.1 jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mytest?useSSL=false
jdbc.username=toutou
jdbc.password=demo123456
1.2 mybatis-generator.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <properties resource="mybatis-config/jdbc.properties"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库链接地址账号密码--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <javaModelGenerator targetPackage="learn.model.po" targetProject="../learn-model/src/main/java/"> <property name="enableSubPackages" value="false" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="mapper" targetProject="../learn-persist/src/main/resources"> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <javaClientGenerator targetPackage="learn.persist" targetProject="../learn-persist/src/main/java/" type="XMLMAPPER"> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!--生成对应表及类名--> <table tableName="useraccount" domainObjectName="UserAccount" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>
mybatis-generator.xml more of the description can be seen here .
1.3 version number of the unified configuration
In order to facilitate the subsequent management, the introduction of all the unified version widget on the project (hellolearn) pom.xml is unified control, each sub pom.xml Module1 (eg learn-persist) the direct use of ${}
the method of use.
For example: in the pom.xml properties hellolearn added < mybatis-generator.version>1.3.6< /mybatis-generator.version>
directly in pom.xml learn-persist in < version>${mybatis-generator.version}< /version>
use to.
Note that the above "example," the code is a space for the tab portion to escape, preventing browser "mybatis-generator.version" and "version" is identified as html tags.
1.4 Maven Project plug-in
As shown above, the maven project idea on the right will be able to find mybatis-generator corresponding plug, if you can not find the learn-persist right maven project, and then click generate sources ....
1.5 running mybatis generator plug-in
As shown above, click operation mybatis generator: generator plug, will generate the corresponding left three files marked red.
v write controller
2.0 Add entity class return result
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
2.1 Adding controller
package learn.web.controller;
import learn.model.vo.Result;
import learn.model.vo.UserAccountVO;
import learn.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author toutou
* @date by 2019/07
*/
@RestController
public class UserController {
@Autowired
UserAccountService userAccountService;
@GetMapping("/user/hello")
public String helloWorld() {
return "hello world.";
}
@GetMapping("/user/getuser")
public Result getUserAccountById(@RequestParam("uid") int id){
UserAccountVO user = userAccountService.getUserAccountById(id);
if(user != null){
return Result.setSuccessResult(user);
}else{
return Result.setErrorResult(404, "用户不存在");
}
}
}
Note: getUserAccountById temporarily do not have access, you can not add.
2.2 Add aplication start class
package learn.web; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; /** * Created by toutou on 2019/7 */ @SpringBootApplication @ComponentScan(basePackages = {"learn.*" }) @MapperScan(basePackages = {"learn.persist"}) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
2.3 Add aplication.properties
server.port=8300
spring.profiles.active=@env@
#mybatis
mybatis.mapper-locations = classpath:mapper/*Mapper.xml
2.4 Configuration startup class
2.5 Test results
v writing service
3.1 Adding Service
package learn.service; import learn.model.vo.UserAccountVO; /** * @author toutou * @date by 2019/07 */ public interface UserAccountService { UserAccountVO getUserAccountById(Integer id); }
3.2 The Service
package learn.service.impl; import learn.model.po.UserAccount; import learn.model.vo.UserAccountVO; import learn.persist.UserAccountMapper; import learn.service.UserAccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @author toutou * @date by 2019/07 */ @Service public class UserAccountServiceImpl implements UserAccountService{ @Autowired UserAccountMapper userMapper; public UserAccountVO getUserAccountById(Integer id){ UserAccountVO accountVO = null; UserAccount account = userMapper.selectByPrimaryKey(id); if (account != null) { accountVO = new UserAccountVO(); accountVO.setId(account.getId()); accountVO.setAccount(account.getAccount()); accountVO.setAge(account.getAge()); accountVO.setEmail(account.getEmail()); accountVO.setUsername(account.getUsername()); accountVO.setPhone(account.getPhone()); } return accountVO; } }
v Configuration Settings
4.1 Add application.properties
4.2 Add application.dev.properties
spring.datasource.url=jdbc:mysql://localhost:3306/mytest?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2b8 spring.datasource.username=toutou spring.datasource.password=demo123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
4.3 Adding pom.xml configuration
4.3.1 hellolearn pom.xml
4.3.2 learn-web pom.xml
<build> <filters> <filter>src/main/resources/config/application-${env}.properties</filter> </filters> </build>
4.4 update application startup class
4.5 Test results
v Linux deployment springboot
5.1 learn-web pom.xml to add plug-ins
<build> <filters> <filter>src/main/resources/config/application-${env}.properties</filter> </filters> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>learn.web.Application</mainClass> <classifier>exec</classifier> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
5.2 hellolearn pom.xml to add plug-ins
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <configuration> <excludes> <exclude>**/profiles/</exclude> <exclude>**/mybatis-generator/</exclude> </excludes> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
Note: If the file directory of useful profiles to exclude.
5.3 Package deployment
java -jar learn-web-0.0.1-SNAPSHOT-exec.jar --server.port=95
Problems you may encounter:
- Spring boot Field required a bean of type that could not be found
- Caused by: java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'xxxController' bean method
- mybatis-generator using the maven plug abnormality (mybatis-generator-maven-plugin): generate failed: Exception getting JDBC Driver
- maven出现:Failed to execute goal on project ...: Could not resolve dependencies for project ...
- Given Execution default of goal org.springframework.boot spring boot when packaging items: spring-boot-maven-plugin: 1.5.
- Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
v Source address
https://github.com/toutouge/javademosecond/tree/master/hellolearn
Author: Please call me brother head
out at: http://www.cnblogs.com/toutou/
About the author: focus on project development foundation platform. If you have questions or suggestions, please enlighten me a lot!
Disclaimer: This article belongs to the author and blog Park total, welcome to reprint, but without the author's consent declared by this section must be retained, and given the original article page link in the apparent position.
Hereby declare: All comments and private messages will reply in the first time. We greatly welcome the garden also correct errors and common progress. Or direct private letter I
in solidarity Bloggers: If you think the article is helpful to you, you can click on the bottom right corner of the article [ recommend ] it. You are encouraged to adhere to the original author continued writing and maximum power!