目录
Springboot 理解:
spring boot其实不是什么新的框架,它默认配置了很多框架的使用方式,就像maven整合了所有的jar包,spring boot整合了许多优秀框架,它能够简单、快速、方便为我们搭建好框架。
接下来进入搭建环节:
- Eclipse新建Maven项目
在pom文件中添加依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 核心模块,包括自动配置支持,日志 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
<!-- 测试模块, 包括 Junit -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.0.5.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- 添加支持web的模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<!-- 但springBoot对调试支持很好,修改之后可以实时生效,需要添加以下的配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.0.5.RELEASE</version>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>bootstrap-test.properties</exclude>
<exclude>bootstrap-dev.properties</exclude>
<exclude>bootstrap-pro.properties</exclude>
<exclude>bootstrap.properties</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>bootstrap-${env}.properties</include>
<include>bootstrap.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
新建自己的包 创建一个名为Application.java的类
package indi.chq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
目录结构如下:(mapper文件夹及application.properties暂未用到)
新建一个Controller类 TestController.java
package indi.chq.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//@RestController的意思就是controller里面的方法都以json格式输出,不用再写什么jackjson配置的了!
@RestController
public class TestController {
@RequestMapping("/test")
public String Demo(){
return "Hello World";
}
}
运行Application.java 看到如下信息表示运行成功!
在浏览器访问地址 localhost:8080/test 访问成功!
接着我们整合mybatis:
1:完善各类包 如下:(UserExample.java不需要)
2:在 application.properties 中添加配置信息
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=indi.chq.entity
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password =
3:entity下代码:
Users.java
package indi.chq.entity;
public class Users {
private Long id;
private String username;
private String password;
private String userSex;
private String nickName;
//省略 get。。。 set。。。
}
4:mapper包下代码
UsersMapper.java
package indi.chq.mapper;
import org.apache.ibatis.annotations.Mapper;
import indi.chq.entity.Users;
@Mapper //注释别忘记
public interface UsersMapper {
Users selectByPrimaryKey(Long id);
}
5:service包下接口声明
TestService.java
package indi.chq.service;
import indi.chq.entity.Users;
public interface TestService {
Users getUserById();
}
6:service.impl包下
TestServiceImpl.java
package indi.chq.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import indi.chq.entity.Users;
import indi.chq.mapper.UsersMapper;
import indi.chq.service.TestService;
@Service
public class TestServiceImpl implements TestService{
@Autowired
private UsersMapper usersMapper;
public Users getUserById(long id) {
return usersMapper.selectByPrimaryKey(id);
}
}
7:编写UsersMapper.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="indi.chq.mapper.UsersMapper" >
<resultMap id="BaseResultMap" type="indi.chq.entity.Users" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="userName" property="username" jdbcType="VARCHAR" />
<result column="passWord" property="password" jdbcType="VARCHAR" />
<result column="user_sex" property="userSex" jdbcType="VARCHAR" />
<result column="nick_name" property="nickName" jdbcType="VARCHAR" />
</resultMap>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
select
*
from users
where id = #{id,jdbcType=BIGINT}
</select>
</mapper>
8:编写Controller类,注入TestService
package indi.chq.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import indi.chq.entity.Users;
import indi.chq.service.TestService;
//@RestController的意思就是controller里面的方法都以json格式输出,不用再写什么jackjson配置的了!
@RestController
public class TestController {
@Autowired
private TestService testService;
@RequestMapping("/queryUsers/{id}")
public Users queryUsers(@PathVariable long id){
return testService.getUserById(id);
}
@RequestMapping("/test")
public String Demo(){
return "Hello World";
}
}
9:运行 Application.java
附加sql脚本
-- ----------------------------
-- Table structure for users
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id',
`userName` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户名',
`passWord` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密码',
`user_sex` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`nick_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;