Spring Boot+MyBatis项目搭建

步骤一、pom.xml依赖jar包配置

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4     <groupId>com.hzit</groupId>
 5     <artifactId>SpringBootTest1</artifactId>
 6     <packaging>war</packaging>
 7     <version>0.0.1-SNAPSHOT</version>
 8     <name>SpringBootTest1 Maven Webapp</name>
 9     <url>http://maven.apache.org</url>
10     <parent>
11         <groupId>org.springframework.boot</groupId>
12         <artifactId>spring-boot-starter-parent</artifactId>
13         <version>1.3.3.RELEASE</version>
14     </parent>
15 
16     <dependencies>
17         <dependency>
18             <groupId>org.springframework.boot</groupId>
19             <artifactId>spring-boot-starter</artifactId>
20         </dependency>
21 
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-starter-web</artifactId>
25         </dependency>
26         <dependency>
27             <groupId>org.mybatis.spring.boot</groupId>
28             <artifactId>mybatis-spring-boot-starter</artifactId>
29             <version>1.1.1</version>
30         </dependency>
31 
32         <dependency>
33             <groupId>org.apache.tomcat.embed</groupId>
34             <artifactId>tomcat-embed-jasper</artifactId>
35             <scope>provided</scope>
36         </dependency>
37         <dependency>
38             <groupId>mysql</groupId>
39             <artifactId>mysql-connector-java</artifactId>
40         </dependency>
41         <!-- 数据库连接池 -->
42         <dependency>
43             <groupId>com.alibaba</groupId>
44             <artifactId>druid</artifactId>
45             <version>1.0.14</version>
46         </dependency>
47         <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
48         <dependency>
49             <groupId>javax.servlet</groupId>
50             <artifactId>jstl</artifactId>
51         </dependency>
52 
53     </dependencies>
54     <build>
55         <finalName>SpringBootTest1</finalName>
56         <plugins>
57             <plugin>
58                 <!-- maven构件需要的JDK默认版本是1.5,和工程中的不是同一个,所以需要构件3.0需要重新指定 -->
59                 <artifactId>maven-compiler-plugin</artifactId>
60                 <configuration>
61                     <source>1.8</source>
62                     <target>1.8</target>
63                     <encoding>utf-8</encoding>
64                 </configuration>
65             </plugin>
66             <plugin>
67                 <groupId>org.springframework.boot</groupId>
68                 <artifactId>spring-boot-maven-plugin</artifactId>
69             </plugin>
70         </plugins>
71     </build>
72 </project>

步骤二、application.properties文件配置

# 配置jsp文件的位置,默认位置为:src/main/webapp
spring.mvc.view.prefix=/WEB-INF/jsp/
# 配置jsp文件的后缀
spring.mvc.view.suffix=.jsp

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useUnicode = true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=root
#如果dao接口名称和路径跟mapper.xml文件不一致的时候可以手动指定位置
#mybatis.mapper-locations=classpath*:mybatis/*Mapper.xml
mybatis.type-aliases-package=com.ycl.sbt.bean

步骤三、编写bean

 1 package com.ycl.sbt.bean;
 2 
 3 public class City {
 4     private int cityId;
 5     private String cityName;
 6     private String zipCode;
 7     private int provinceId;
 8 
 9     public int getCityId() {
10         return cityId;
11     }
12 
13     public void setCityId(int cityId) {
14         this.cityId = cityId;
15     }
16 
17     public String getCityName() {
18         return cityName;
19     }
20 
21     public void setCityName(String cityName) {
22         this.cityName = cityName;
23     }
24 
25     public String getZipCode() {
26         return zipCode;
27     }
28 
29     public void setZipCode(String zipCode) {
30         this.zipCode = zipCode;
31     }
32 
33     public int getProvinceId() {
34         return provinceId;
35     }
36 
37     public void setProvinceId(int provinceId) {
38         this.provinceId = provinceId;
39     }
40 
41     @Override
42     public String toString() {
43         return "City [cityId=" + cityId + ", cityName=" + cityName + ", zipCode=" + zipCode + ", provinceId="
44                 + provinceId + "]";
45     }
46 
47 }

步骤四、编写dao层

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 
5 <mapper namespace="com.ycl.sbt.dao.CityMapper">
6     <select id="getCityListByPid" resultType="City" >
7         select cityId,cityName,zipCode,provinceId from s_city where provinceId=#{provinceId}
8     </select>
9 </mapper>
1 package com.ycl.sbt.dao;
2 
3 import java.util.List;
4 
5 import com.ycl.sbt.bean.City;
6 
7 public interface CityMapper {
8     public List<City> getCityListByPid(String provinceId);
9 }

步骤五、编写service

1 package com.ycl.sbt.service;
2 
3 import java.util.List;
4 
5 import com.ycl.sbt.bean.City;
6 
7 public interface ICityService {
8     public List<City> getCityListByPid(String provinceId);
9 }
 1 package com.ycl.sbt.service.impl;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Service;
 7 
 8 import com.ycl.sbt.bean.City;
 9 import com.ycl.sbt.dao.CityMapper;
10 import com.ycl.sbt.service.ICityService;
11 
12 @Service
13 public class CityServiceImpl implements ICityService {
14 
15     @Autowired
16     private CityMapper CityMapper;
17 
18     @Override
19     public List<City> getCityListByPid(String provinceId) {
20         return CityMapper.getCityListByPid(provinceId);
21     }
22 
23 }

步骤六、编写controller

 1 package com.ycl.sbt.controller;
 2 
 3 import java.util.List;
 4 
 5 import org.mybatis.spring.annotation.MapperScan;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 8 import org.springframework.context.annotation.ComponentScan;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RestController;
11 
12 import com.ycl.sbt.bean.City;
13 import com.ycl.sbt.service.ICityService;
14 
15 @EnableAutoConfiguration
16 @RestController
17 @ComponentScan("com.ycl.sbt.service")
18 @MapperScan("com.ycl.sbt.dao")
19 public class CityController {
20 
21     @Autowired
22     private ICityService cityService;
23 
24     @RequestMapping("getCityListByPid")
25     public List<City> getCityListByPid(String provinceId) {
26         return cityService.getCityListByPid(provinceId);
27     }
28 }

步骤七、编写启动程序

 1 package com.ycl.sbt.controller;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 
 6 @SpringBootApplication
 7 public class HelloBoot {
 8     public static void main(String[] args) {
 9         SpringApplication.run(HelloBoot.class, args);
10     }
11 }
12  

步骤八、访问

http://localhost:8080/getCityListByPid?provinceId=1

[{"cityId":1,"cityName":"太原","zipCode":"11111","provinceId":1},{"cityId":7,"cityName":"太原县","zipCode":"11211","provinceId":1}]

 

posted @ 2017-09-03 22:51  fengqixueluo  阅读(1057)  评论(0编辑  收藏  举报