1、application.yml配置文件
server:
port: 8080
servlet:
path: "*.do"
#zeroDateTimeBehavior=CONVERT_TO_NULL ,在查询数据库时有空值,对应实体类中的赋值为null
spring:
datasource:
url: jdbc:mysql://localhost:3306/netctoss?useSSL=true&characterEncoding=utf-8&serverTimezone=UTC&zeroDateTimeBehavior=CONVERT_TO_NULL
password: 123456
username: root
mvc:
view:
prefix: ""
suffix: ".html"
#开启驼峰映射
mybatis:
configuration:
map-underscore-to-camel-case: true
#启动日志记录
logging:
level:
com.tedu.demo: debug
2、设置项目的启动页
编写springmvc默认配置(默认启动index.html),而我想要启动项目就展示login.html
方法:编写一个类,添加@Configuration注解实现WebMvcConfigurer接口,重写addViewControllers方法
@Configuration
public class DefaultController implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/login.html");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
}
3、扫描mapper接口
在启动类上加上@MapperScan(“mapper路径”)
4、使用一些特别的依赖
通用mapper依赖
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.3</version>
</dependency>
使用lombok(idea添加插件)
给实体类加注解
@Data(get,set)
@NoArgsConstructor(空参构造)
@AllArgsConstructor(有参构造)
给主键id添加注解
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
5、使用ajax前后端交互时取值为undefined
controller中返回json字符串,在前端需要将json字符串转为json对象,再取值
使用JSONObject(包import net.sf.json.JSONObject;)将结果封装为json字符串
jsonObjec.put(“result”,“登录成功”),返回return jsonObject.toString();
6、springboot整合mybatis时绑定失败
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
问题就是mapper.xml找不到对应的接口,检查namespace对应的接口名和id对应方法名
首先在application.yml中设置mapper.xml文件位置
编写mapper.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.tedu.demo.mapper.AdminMapper">
<select id="queryAll" resultType="com.tedu.demo.pojo.Admin">
select * from admin_info
</select>
</mapper>