1、由于使用的是Idea社区版,这里从头搞一下,我下载的Idea社区版压缩包,解压缩以后,直接打开就行了。
点击Configure -> Plugins插件。旗舰版Idea自带有spring Initializr,社区版本是啥子都没有默认安装的,这里安装这个spring Assistant插件,在settings中plugins中搜索进行安装。弹出Accept,点击接受就行了,然后Restart IDE。
2、然后点击Create New Project,创建我们的项目,如下所示:
由于我是新下载的社区版Idea,这里我没有使用他们默认的jdk11,我使用本地jdk1.8版本。
3、然后这些组织号,项目名称,版本号,项目构建方式,语言,打包方式,语言版本,项目名称,项目描述,包名称,根据自己喜好去搞吧。
4、选择项目所需要的依赖,这里由于要连接Mysql数据库,所以要将这些依赖都加进去的。
5、修改你的项目名称和存储的位置,保存即可。
6、设置字体大小,字小看着真是不舒服的。
7、Idea社区版,设置一下maven。可以安装一下Maven Runner插件。
8、Idea社区版,集成tomcat,果然免费的,都是要自己手动搞的,有点浪费时间了。
安装好插件以后,重启Idea。在右上角Edit Configurations 配置环境变量,如下所示。
然后点击configuration进行配置tomcat。看到上面那一行英文了吗,Template,The values saved here will be used for new configurations of the same type,然后点击Create configuration哦。
Tomcat添加成功以后,点击Apply,点击Ok即可了。
最后再配置一下Tomcat即可。
1)、Name : 项目名称。 2)、Tomcat Server: tomcat的路径。 3)、Deployment Directory: webapps的路径。在main文件下创建一个新的目录文件webapps。注意此处需要填写的路径是源码里webapps的路径。 4)、custom context: 自定义上下文,这里不自定义了。 5)、Context Path : 上下文路径,这个会自动识别,一般不用修改。发布的上下文,即访问url的前面的根路径(会自动识别,一般不需要修改)。 6)、Server Port : 服务器监听端口 8080 (一般自行修改)。 7)、VM options : Java虚拟机参数设置(可不填)。
配置完成之后,点击右上角的三角运行按钮就能正常启动tomcat了。
9、Maven依赖包,如下所示:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5 <parent>
6 <groupId>org.springframework.boot</groupId>
7 <artifactId>spring-boot-starter-parent</artifactId>
8 <version>2.2.5.RELEASE</version>
9 <relativePath/> <!-- lookup parent from repository -->
10 </parent>
11 <groupId>com.bie</groupId>
12 <artifactId>demo</artifactId>
13 <version>0.0.1-SNAPSHOT</version>
14 <name>demo</name>
15 <description>Demo project for Spring Boot</description>
16
17 <properties>
18 <java.version>1.8</java.version>
19 </properties>
20
21 <dependencies>
22 <dependency>
23 <groupId>org.springframework.boot</groupId>
24 <artifactId>spring-boot-starter-data-jdbc</artifactId>
25 </dependency>
26 <dependency>
27 <groupId>org.springframework.boot</groupId>
28 <artifactId>spring-boot-starter-jdbc</artifactId>
29 </dependency>
30 <dependency>
31 <groupId>org.springframework.boot</groupId>
32 <artifactId>spring-boot-starter-web</artifactId>
33 </dependency>
34 <dependency>
35 <groupId>org.mybatis.spring.boot</groupId>
36 <artifactId>mybatis-spring-boot-starter</artifactId>
37 <version>2.1.1</version>
38 </dependency>
39
40 <dependency>
41 <groupId>mysql</groupId>
42 <artifactId>mysql-connector-java</artifactId>
43 <scope>runtime</scope>
44 </dependency>
45 <dependency>
46 <groupId>org.projectlombok</groupId>
47 <artifactId>lombok</artifactId>
48 <optional>true</optional>
49 </dependency>
50 <dependency>
51 <groupId>org.springframework.boot</groupId>
52 <artifactId>spring-boot-starter-test</artifactId>
53 <scope>test</scope>
54 <exclusions>
55 <exclusion>
56 <groupId>org.junit.vintage</groupId>
57 <artifactId>junit-vintage-engine</artifactId>
58 </exclusion>
59 </exclusions>
60 </dependency>
61 </dependencies>
62
63 <build>
64 <plugins>
65 <plugin>
66 <groupId>org.springframework.boot</groupId>
67 <artifactId>spring-boot-maven-plugin</artifactId>
68 </plugin>
69 </plugins>
70 </build>
71
72 </project>
10、你可以使用application.properties文件,也可以使用application.yml文件。我这里使用application.properties文件,创建了两个properties文件application.properties、application-dev.properties。或者你可以根据坏境多创建几个配置文件的。嗯,好吧,社区版配置文件提示都没有,毛线啊,难搞哦!
在项目中配置多套环境的配置方法。因为现在一个项目有好多环境,开发环境,测试环境,生产环境,每个环境的参数不同,所以我们就可以把每个环境的参数配置到properties文件中,这样再想用哪个环境的时候只需要在主配置文件中将用的配置文件写上就行。在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如: application-dev.properties:开发环境。 application-test.properties:测试环境。 application-prod.properties:生产环境。 至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应{profile}值。
application.properties配置文件,如下所示:
1 spring.profiles.active=dev
application-dev.properties配置文件,如下所示:
1 server.port=8080
2 server.address=127.0.0.1
3 # mysql的驱动连接
4 spring.datasource.username=root
5 spring.datasource.password=123456
6 spring.datasource.url=jdbc:mysql://localhost:3306/gov_policy?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
7 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
8 # mybatis
9 mybatis.mapper-locations=classpath:mapping/*Mapper.xml
10 mybatis.type-aliases-package=com.bie.demo.po
11 # 后台打印sql语句
12 mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
10、创建包,开始码代码。resources下创建mapping文件夹,用于配置sql语句,也可以用注解的方式直接写在mapper文件里。看个人喜好了这里。
实体类CustomerInfo ,如下所示:
1 package com.bie.demo.po;
2
3 import lombok.AllArgsConstructor;
4 import lombok.Data;
5 import lombok.NoArgsConstructor;
6 import lombok.ToString;
7
8 /**
9 *
10 */
11 @Data
12 @ToString
13 @AllArgsConstructor
14 @NoArgsConstructor
15 public class CustomerInfo {
16
17 private int id;
18 private String account;
19 private String cname;
20 private String sex;
21 private String password;
22 private String identity;
23 private String telephone;
24 private String address;
25 private String birthday;
26 private String identification;
27
28 }
数据交互层CustomerInfoMapper接口,如下所示:
1 package com.bie.demo.mapper;
2
3 import com.bie.demo.po.CustomerInfo;
4 import org.springframework.stereotype.Repository;
5
6 @Repository
7 public interface CustomerInfoMapper {
8
9 /**
10 * 根据政策ID查询
11 *
12 * @param id
13 * @return
14 */
15 public CustomerInfo selectCustomerInfoById(int id);
16 }
业务逻辑层接口,和实现类,如下所示:
1 package com.bie.demo.service;
2
3 import com.bie.demo.po.CustomerInfo;
4
5 /**
6 *
7 */
8 public interface CustomerInfoService {
9
10 /**
11 * 根据政策ID查询
12 *
13 * @param id
14 * @return
15 */
16 public CustomerInfo selectCustomerInfoById(int id);
17 }
1 package com.bie.demo.service.impl;
2
3 import com.bie.demo.mapper.CustomerInfoMapper;
4 import com.bie.demo.po.CustomerInfo;
5 import com.bie.demo.service.CustomerInfoService;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.stereotype.Service;
8
9 /**
10 *
11 */
12 @Service
13 public class CustomerInfoServiceImpl implements CustomerInfoService {
14
15 @Autowired
16 private CustomerInfoMapper customerInfoMapper;
17
18 @Override
19 public CustomerInfo selectCustomerInfoById(int id) {
20 if (id > 0) {
21 CustomerInfo customerInfo = customerInfoMapper.selectCustomerInfoById(id);
22 if (customerInfo != null) {
23 return customerInfo;
24 }
25 }
26 return null;
27 }
28 }
控制层CustomerInfoController,如下所示:
1 package com.bie.demo.controller;
2
3 import com.bie.demo.po.CustomerInfo;
4 import com.bie.demo.service.CustomerInfoService;
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.stereotype.Controller;
7 import org.springframework.web.bind.annotation.RequestMapping;
8 import org.springframework.web.bind.annotation.RequestParam;
9 import org.springframework.web.bind.annotation.ResponseBody;
10
11 /**
12 *
13 */
14 @Controller
15 @RequestMapping(value = "/customerInfo")
16 public class CustomerInfoController {
17
18 @Autowired
19 private CustomerInfoService customerInfoService;
20
21 @RequestMapping(value = "/selectCustomerInfoById")
22 @ResponseBody
23 public CustomerInfo selectCustomerInfoById(@RequestParam(value = "id") int id) {
24 CustomerInfo customerInfo = customerInfoService.selectCustomerInfoById(id);
25 if (customerInfo != null) {
26 return customerInfo;
27 }
28 return null;
29 }
30
31
32 }
实体类和数据表映射配置文件CustomerInfoMapper.xml,如下所示:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
4 <mapper namespace="com.bie.demo.mapper.CustomerInfoMapper">
5
6 <resultMap id="BaseResultMap" type="com.bie.demo.po.CustomerInfo">
7 <result column="id" jdbcType="INTEGER" property="id"/>
8
9 <result column="account" jdbcType="VARCHAR" property="account"/>
10 <result column="cname" jdbcType="VARCHAR" property="cname"/>
11 <result column="sex" jdbcType="VARCHAR" property="sex"/>
12 <result column="password" jdbcType="VARCHAR" property="password"/>
13 <result column="telephone" jdbcType="VARCHAR" property="telephone"/>
14 <result column="address" jdbcType="VARCHAR" property="address"/>
15 <result column="birthday" jdbcType="VARCHAR" property="birthday"/>
16 <result column="identification" jdbcType="VARCHAR" property="identification"/>
17 </resultMap>
18
19 <select id="selectCustomerInfoById" resultMap="BaseResultMap">
20 select
21 *
22 from
23 customer_info
24 where
25 id = #{id}
26 </select>
27
28 </mapper>
SpringBoot项目启动类,如下所示:
切记,一定要配置,需要扫描的mapper文件路径,不然报错哦,之前贴过,这里略过,@MapperScan("com.bie.demo.mapper")。
1 package com.bie.demo;
2
3 import org.mybatis.spring.annotation.MapperScan;
4 import org.springframework.boot.SpringApplication;
5 import org.springframework.boot.autoconfigure.SpringBootApplication;
6
7 @MapperScan("com.bie.demo.mapper")
8 @SpringBootApplication
9 public class DemoApplication {
10
11 public static void main(String[] args) {
12 SpringApplication.run(DemoApplication.class, args);
13 }
14
15 }
11、项目结构图,如下所示:
12、启动项目,测试如下所示:
13、数据表结构,如下所示:
14、如何使用Mybatis的sql语句注解版呢,如下所示:
application-dev.properties配置文件,修改为如下所示:
1 server.port=8080
2 server.address=127.0.0.1
3 # mysql的驱动连接
4 spring.datasource.username=root
5 spring.datasource.password=123456
6 spring.datasource.url=jdbc:mysql://localhost:3306/biehl?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
7 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
8 # mybatis
9 # mybatis.mapper-locations=classpath:mapping/*Mapper.xml
10 # mybatis依赖
11 mybatis.type-aliases-package=com.bie.demo.mapper
12 # 后台打印sql语句
13 mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
数据交互层,CustomerInfoMapper,修改为如下所示:
1 package com.bie.demo.mapper;
2
3 import com.bie.demo.po.CustomerInfo;
4 import org.apache.ibatis.annotations.Select;
5 import org.springframework.stereotype.Repository;
6
7 @Repository
8 public interface CustomerInfoMapper {
9
10 /**
11 * 根据政策ID查询
12 *
13 * @param id
14 * @return
15 */
16 @Select("SELECT * FROM customer_info WHERE id = #{id}")
17 public CustomerInfo selectCustomerInfoById(int id);
18 }
此时,将resources目录下面的mapping目录删除即可。通过测试,表示是可行的。