1 创建springboot工程
引入依赖: springboot,mybatis,mysql-jdbc,连接池 druid,test
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot集成Junit 的启动依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 阿里巴巴的Druid数据源依赖启动器 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- MyBatis依赖启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- MySQL数据库连接驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
2.配置文件
application.properties,application.yaml
配置内容
- 数据源
连接路径,账号,密码,驱动
- 数据库连接池
最大连接数,最小连接数,连接超时
- mybatis
别名,驼峰 还可以配置mapper位置,配置mybatis日志为debug
- 如果引入thymleaft
在测试阶段 关闭缓存,注意创建template目录
- 配置端口号
#配置端口号
server.port=8081
#数据源
spring.datasource.url=jdbc:mysql://localhost:3306/szqy08?serverTimezone=UTC&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
#连接池
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.max-active=10
spring.datasource.druid.initial-size=5
#mybatis 相关配置
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.type-aliases-package=com.aaa.element.entity
3.创建启动器
启动器必须在包名根目录
1.在启动器配置 @SpringBootApplication
2.SpringApplication.run(ElementApplication.class,args);
@SpringBootApplication
public class ElementApplication {
public static void main(String[] args) {
SpringApplication.run(ElementApplication.class,args);
}
}
4.创建实体类 entity
数据表中每行对应一个实体类
1.setter getter方法
2.toString()
3.无参构造 (如果有有参构造,必须写无参构造)
4.实现序列化serialze(为什么? 1.将对象保存在本地磁盘 2.利于对象网络传输(rpc–netty))
注意的问题:
属性名和数据名一定要确认
如果数据库名是下划线,mybatis开启驼峰,属性名必须驼峰
public class Student implements Serializable {
private int id;
private String name;
private int age;
private String sex;
private float height;
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
private List<String> hoppy;
private Map<String,String> course;
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Map<String, String> getCourse() {
return course;
}
public void setCourse(Map<String, String> course) {
this.course = course;
}
public List<String> getHoppy() {
return hoppy;
}
public void setHoppy(List<String> hoppy) {
this.hoppy = hoppy;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", hoppy=" + hoppy +
", course=" + course +
", email='" + email + '\'' +
'}';
}
}
5.创建mapper dao层
- 1.注解的dao
简单增删该查
- 2.基于xml
复杂的sql (表关联,一对多,一对一),在实体类和数据字段不对应时
注意点:
1.对应接口要@Mapper 在springboot中有报错 @Repository
2.如果传入的参数 是多个 1.把多个封为实体类 2.使用@param
3.当基于xml 实现时, mapper的xml文件和mapper接口 文件结构不对应,有没有在配置文件声明
@Mapper
@Repository
public interface StudentMapper {
@Select("select * from student_tb where id = #{id}")
Student findStudentById(int id);
@Select("select * from student_tb")
List<Student> findAllStudent();
@Update("update student_tb set name = #{name},age=#{age},sex=#{sex},height=#{height} where id = #{id}")
int updateStudent(Student student);
@Delete("delete from student_tb where id = #{id}")
int deleteStudent(int id);
@Insert("insert into student_tb (name,age,sex,height) values(#{name},#{age},#{sex},#{height})")
int addStudent(Student student);
}
6.创建service
1.使用@service 标记实现类,加入到容器
2.使用@Autowire @Resources 注入dao层
3.service 要有接口以及实现类 (面向接口编程,实现多态-----》提高扩展性,开闭原则)
7.创建controller
1.加入容器 @Controller
@RestController == @Controller + @ResponseBody (见整controller类中所有返回的数据转换为json)
2.配置映射
@RequestMapping
@GetMapping == RequestMapping + method=get
@PostMapping
3.处理器返回值 有几种
1.String
返回的路径
springmvc 返回的是jsp 文件路径,如果配置了视图解析器返回的是文件名
springboot 返回的thymleaft 文件名(spring boot已经配置好了)
2.modelAndView
在springmvc中返回modelAndView (数据+ 视图名)
3.void
在springmvc中返回空数据
4.返回 实体
返回json 数据,一定用记得使用@ResponsBody进行标记,
8.先测试接口
先通过浏览器将所有接口调通,再写前端界面
分步骤,分模块
这样子遇到bug 解决问题,通过分段排除,数据最快
9.引入前端依赖
一定要注意:
1注意vue 和elementUi,axios 版本的兼容
2.在html中引入依赖 首先引入vue,在引入其他的
3.依赖要放至到public,static目录,必须防止这目录,否则就被当作路径被dispatchServlet拦截处理