此处使用IDEA快速搭建SpringBoot应用,首先用SpringBoot搭建WEB工程:
然后点击Next生成项目,首次生成可能有点慢,下次创建的时候就会快很多,生成后的目录结构如下:
我们更改下这个启动类:
加上@RestController注解使之成为一个Controller,请求能进来,然后加上相应的方法:
@SpringBootApplication
@RestController
public class SpringbootdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootdemoApplication.class, args);
}
@RequestMapping(value = "/",produces = "text/plain;charset=UTF-8")
String index(){
return "Hello Spring Boot!";
}
}
点击右上角的启动项,启动这个项目:
然后在浏览器中输入地址:http://localhost:8080/ 可以看到如下效果:
到此,一个简单的SpringBoot搭建完成了
2.然后开始我们的第二步,搭建SSM
在上一步的基础上我们需要添加些相关的配置来搭建SSM,如下为结构图:
UserController:
@RestController
/**自动返回的是json格式数据***/
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("list")
public List<User> list(){
//只对紧邻的下一条select语句进行分页查询,对之后的select不起作用
List<User> list = userService.findAllUser();
return list;
}
}
User:
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
UserMapper:
@Mapper
public interface UserMapper {
public List<User> findAll();
}
UserService:
public interface UserService {
/**
* 根据接口查询所用的用户
*/
public List<User> findAllUser();
}
UserServiceImpl:
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
public List<User> findAllUser() {
List<User> list = userMapper.findAll();
return list;
}
}
UserMapper.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.demo.mapper.UserMapper">
<select id="findAll" resultType="com.demo.bean.User">
select * from user
</select>
</mapper>
SqlMapperConfig:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- <settings> 开启二级缓存 <setting name="cacheEnabled" value="true" /> </settings> -->
<!-- 只需要定义个别名,这个应该有 -->
<!-- springboot集成pageHelper(Java配置未注入的时候可以用下面配置) -->
<!-- <plugins>
com.github.pagehelper为PageHelper类所在包名
<plugin interceptor="com.github.pagehelper.PageInterceptor">
使用下面的方式配置参数,后面会有所有的参数介绍
<property name="helperDialect" value="mysql" />
</plugin>
</plugins> -->
</configuration>
application.properties:
#server.port=80
logging.level.org.springframework=DEBUG
#springboot mybatis
#jiazai mybatis peizhiwenjian
mybatis.mapper-locations = classpath:mapper/*Mapper.xml
mybatis.config-location = classpath:mybatis/sqlMapConfig.xml
#mybatis.type-aliases-package = com.demo.bean
#shujuyuan
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = ****
spring.datasource.password = ****
Pom.XML:
<?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.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>springbootdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springbootdemo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--mybatis与mysql-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--druid依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.25</version>
</dependency>
<!--redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--jasypt加解密-->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>1.14</version>
</dependency>
<!--zipkin-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
<!--eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
项目运行后访问http://localhost:8080/list,可以见到如下结果(这里省略sql表的设计,可以自己根据实体类来建表)