`

springBoot学习笔记

 
阅读更多

Hello World 之pom.xml配置:
<!--
spring boot 父节点依赖,引入这个之后相关的引入就不需要添加version配置,spring boot会自动选择最合适的版本进行添加。
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>


java.version 指定jdk版本号:
<java.version>1.8</java.version>

<!-- 添加spring-boot-starter-web依赖
该依赖提供了MVC,AOP的依赖包
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

Hello World 之coding
Codeing 步骤:
新建一个Controller类
@RestController注解
等价于@Controller 和 @ResponseBody
@RequestMapping注解  

新建启动类(App – Main方法)
@SpringBootApplication注解    指定这是一个spring boot的应用程序

测试代码
http://localhost:8080/

spring boot返回json数据
默认使用的json解析框架是jackson

第3节 Spring Boot完美使用FastJson解析JSON数据
1.引入fastjson依赖库
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>

这里要说下很重要的话,官方文档说的1.2.10以后,会有两个方法支持HttpMessageconvert,
一个是FastJsonHttpMessageConverter,支持4.2以下的版本,
一个是FastJsonHttpMessageConverter4支持4.2以上的版本,具体有什么区别暂时没有深入研究。
这里也就是说:低版本的就不支持了,所以这里最低要求就是1.2.10+。

2.配置fastjon(支持两种方法)
第一种方法就是:
(1)启动类继承extends WebMvcConfigurerAdapter
(2)重写覆盖方法configureMessageConverters
第一种方式代码:
@SpringBootApplication
public class ApiCoreApp  extends WebMvcConfigurerAdapter {
//1.需要先定义一个convert 转换消息的对象
//2.添加fastJson的配置信息,比如:是否要格式化返回的json数据
//3.在convert中添加配置信息
//4.将convert添加到converters当中
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);

FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat
);
fastConverter.setFastJsonConfig(fastJsonConfig);

converters.add(fastConverter);
}
//启动应用程序的main方法
public static void main(String[] args){
SpringApplication.run(ApiCoreApp.calss,args);
}
}
第二种方法
1)在App.java启动类中,
注入Bean : HttpMessageConverters
第二种方式代码
@SpringBootApplication
public class ApiCoreApp{
//1.需要先定义一个convert 转换消息的对象
//2.添加fastJson的配置信息,比如:是否要格式化返回的json数据
//3.在convert中添加配置信息
//4.将convert添加到converters当中

@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
//启动应用程序的main方法
public static void main(String[] args){
SpringApplication.run(ApiCoreApp.calss,args);
}
}
FastJson在实体bean属性上常用注解
@JSONField(format="yyy-MM-dd HH:mm:ss")
private Date createTime;

@JSONField(serialize=false)     //该属性不进行序列化
private String remarks;

第4节 Spring Boot热部署(springloader)
使用springloaded进行热部署,但是些代码修改了,并不会进行热部署。
在pom.xml文件添加依赖包:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin </artifactId>
<dependencies> 
   <!--springloaded  hot deploy --> 
   <dependency> 
   <groupId>org.springframework</groupId> 
   <artifactId>springloaded</artifactId> 
   <version>1.2.4.RELEASE</version>
   </dependency> 
</dependencies> 
<executions> 
   <execution> 
   <goals> 
   <goal>repackage</goal> 
   </goals> 
   <configuration> 
   <classifier>exec</classifier> 
   </configuration> 
   </execution> 
</executions>
</plugin>
运行方法一:mvn spring-boot:run
存在的问题:
虽然关闭了启动程序,但是进程并没有关闭,
所以在此启动的话会报错,tomcat端口被占用,
要手动把进程杀掉。java.exe
运行方法二:
如果使用的run as – java application的话,那么还需要做一些处理。
把spring-loader-1.2.4.RELEASE.jar下载下来,放到项目的lib目录中,
然后把IDEA的run参数里VM参数设置为:
-javaagent:.\lib\springloaded-1.2.4.RELEASE.jar -noverify
然后启动就可以了,这样在run as的时候,也能进行热部署

存在的问题:
修改值可以热部署,但是添加方法就不行了

第5节 springboot + devtools(热部署)
spring-boot-devtools 是一个为开发者服务的一个模块,
其中最重要的功能就是自动应用代码更改到最新的App上面去。
原理是在发现代码有更改之后,重新启动应用,但是速度比手动停止后再启动还要更快,
更快指的不是节省出来的手工操作的时间。
其深层原理是使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包),
另一个ClassLoader加载会更改的类,称为  restart ClassLoader
,这样在有代码更改的时候,原来的restart ClassLoader 被丢弃,
重新创建一个restart ClassLoader,由于需要加载的类相比较少,所以实现了较快的重启时间(5秒以内)。

1.添加依赖包:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
   <scope>true</scope>
</dependency>

2.添加spring-boot-maven-plugin:
<build>
<plugins>
    <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
          <!--fork :  如果没有该项配置,devtools不会起作用,即应用不会restart -->
                <fork>true</fork>
            </configuration>
        </plugin>
</plugins>
   </build>
说明:
1. devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机),
注意:因为其采用的虚拟机机制,该项重启是很快的。
2. devtools可以实现页面热部署(即页面修改后会立即生效,这个可以直接在application.properties文件中配置spring.thymeleaf.cache=false来实现(这里注意不同的模板配置不一样)。

当修改了方法的返回值,能够进行热部署
当重新创建一个方法,能够进行热部署
当重新创建一个类,能够进行热部署



不能使用分析
对应的spring-boot版本是否正确,这里使用的是1.4.1版本;
是否加入plugin以及属性<fork>true</fork>
Eclipse Project 是否开启了Build Automatically(我自己就在这里栽了坑,不知道为什么我的工具什么时候关闭了自动编译的功能)。
如果设置SpringApplication.setRegisterShutdownHook(false),则自动重启将不起作用。

第7节 Spring Boot JPA-Hibernate

(1)在pom.xml添加mysql,spring-data-jpa依赖;
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

(2)在application.properties文件中配置mysql连接配置文件;
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10

(3)在application.properties文件中配置JPA配置信息;
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
#[org.hibernate.cfg.ImprovedNamingStrategy  #org.hibernate.cfg.DefaultNamingStrategy]
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

(4)编写测试例子;
(1) 创建实体类Demo,如果已经存在,可以忽略。
@Entity     //映射表
@Id        //映射注解
@GeneratedValue(strategy=GenerationType.AUTO)  //指定主键生成策略,自增长

(2) 创建jpa repository类操作持久化(CrudRepository)。
CrudRepository: mainly provides CRUD functions.(主要提供增删改查功能。)
PagingAndSortingRepository: provide methods to do pagination and sorting records.(做分页提供方法和排序记录。)
JpaRepository: provides some JPA related method such as flushing the persistence context and delete record in a batch.(提供一些JPA相关方法如冲洗持久化上下文删除一批记录。)

JpaRepository extends PagingAndSortingRepository which in turn extends CrudRepository.
Because of the inheritance mentioned above,  JpaRepository will have all the functions of CrudRepository and PagingAndSortingRepository.
So if you don't need the repository to have the functions provided by JpaRepository and PagingAndSortingRepository , use CrudRepository.
1.创建Dao接口继承JpaRepository接口或者CrudRepository接口,
该接口有泛型<T , ID>,T参数使用Model类型

2.paRepository接口对查询只提供了findAll以及findOne方法,并没有提供根据某个字段(主键除外)进行查询的功能.

自定义根据指定字段进行查询,方法名需要符合JPA的规范.

在Dao中直接声明方法:

findByXXX();

在ServiceImpl中直接调用即可.
(3) 创建service类。
@Service
@Resource     //注入dao
@Transactional 注解,用于添加事务,要么都成功,要么都失败


(4) 创建restful请求类。
(5) 测试;


第8节 Spring Boot Spring Data JPA介绍
Repository接口
Repository 接口是 Spring Data 的一个核心接口,它不提供任何方法,开发者需要在自己定义的接口中声明需要的方法 :
public interface Repository<T, ID extends Serializable> { }

1. Repository是一个空接口,即是一个标记接口;
2. 若我们定义的接口继承了Repository,则该接口会被IOC容器识别为一个Repository Bean纳入到IOC容器中,进而可以在该接口中定义满足一定规范的方法。
3. 实际上也可以通过@RepositoryDefinition,注解来替代继承Repository接口。
4. 查询方法以find | read | get开头;
5. 涉及查询条件时,条件的属性用条件关键字连接,要注意的是条件属性以首字母大写。
列子:
//根据name进行查询:
public Student findByName(String name);
6.使用@Query注解可以自定义JPQL语句实现更灵活的查询。
列子:
//JPQL语句 类似HQL语句
@Query("from Student where name=:cn")
public Student findByMyName(@Param("cn")String name);


CrudRepository接口
CrudRepository 接口提供了最基本的对实体类的添删改查操作
--T save(T entity);//保存单个实体? ?
    --Iterable<T> save(Iterable<? extends T> entities);//保存集合??????? ?
--T findOne(ID id);//根据id查找实体???????? ?
--boolean exists(ID id);//根据id判断实体是否存在???????? ?
--Iterable<T> findAll();//查询所有实体,不用或慎用!???????? ?
--long count();//查询实体数量???????? ?
--void delete(ID id);//根据Id删除实体???????? ?
--void delete(T entity);//删除一个实体? ?
--void delete(Iterable<? extends T> entities);//删除一个实体的集合???????? ?
--void deleteAll();//删除所有实体,不用或慎用!? ?

PagingAndSortingRepository接口
该接口提供了分页与排序功能  
--Iterable<T> findAll(Sort sort); //排序   
--Page<T> findAll(Pageable pageable); //分页查询(含排序功能)

JpaRepository:查找所有实体,排序、查找所有实体,执行缓存与数据库同步

JpaSpecificationExecutor:不属于Repository体系,实现一组 JPA Criteria 查询相关的方法,封装  JPA Criteria 查询条件。
通常使用匿名内部类的方式来创建该接口的对象。

自定义 Repository:可以自己定义一个MyRepository接口。


第9节 Spring Boot JdbcTemplate

(1) 在pom.xml加入jdbcTemplate的依赖;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

如果在JPA已经加入的话,则可以不用引入以上的配置。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

(2) 编写DemoDao类,声明为:@Repository,引入JdbcTemplate

public Demo getById(long id){
String sql = "select *from Demo where id=?";
RowMapper<Demo> rowMapper = new BeanPropertyRowMapper<Demo>(Demo.class);
return jdbcTemplate.queryForObject(sql, rowMapper,id);
}

(3) 编写DemoService类,引入DemoDao进行使用
@Resource
private DemoDao demoDao;

public void save(Demo demo){
demoRepository.save(demo);
}

(4) 编写Demo2Controller进行简单测试。
@Resource
private DemoService demoService;

@RequestMapping("/getById")
public Demo getById(long id){
return demoService.getById(id);
}

第10节 hello world访问404
1.确认访问地址是否正确:
1)确认端口号,默认是8080,这个可以在启动的控制台进行查看
2)确认访问的URI地址是否填写正确,这个在启动的控制台查看是否被映射了
2.确认注解是否正确,使用@RestController
@RestController注解 等价于@Controller 和 @ResponseBody
3.确认包的路径是否正确
Spring Boot 默认情况下可以扫描到的是
@SpringBootApplication所在的类的同包或者子包下的类
4.确认类引入的包的路径是否正确
import org.springframework.web.bind.annotation.Restcontroller;
import org.springframework.web.bind.annotation.RequestMapping;

第11节 配置server信息
如果想要进行更改的话,只需要修改applicatoin.properties文件,在配置文件中加入:

1.修改端口号
server.port=8081

2.修改context-path
server.context-path=/spring-boot

3.其他配置说明
#server.port=8080
#server.address= # bind to a specific NIC
#server.session-timeout= # session timeout in seconds
#the context path, defaults to '/'
#server.context-path=/spring-boot
#server.servlet-path= # the servlet path, defaults to '/'
#server.tomcat.access-log-pattern= # log pattern of the access log
#server.tomcat.access-log-enabled=false # is access logging enabled
#server.tomcat.protocol-header=x-forwarded-proto # ssl forward headers
#server.tomcat.remote-ip-header=x-forwarded-for
#server.tomcat.basedir=/tmp # base dir (usually not needed, defaults to tmp)
#server.tomcat.background-processor-delay=30; # in seconds
#server.tomcat.max-threads = 0 # number of threads in protocol handler
#server.tomcat.uri-encoding = UTF-8 # character encoding to use for URL decoding

第13节 spring boot使用thymeleaf

(1)在pom.xml中引入thymeleaf;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

(2)如何关闭thymeleaf缓存
########################################################
###THYMELEAF (ThymeleafAutoConfiguration)
########################################################
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
# ;charset=<encoding> is added
#spring.thymeleaf.content-type=text/html
# set to false for hot refresh
#开发过程关闭缓存
spring.thymeleaf.cache=false

(3)编写模板文件.html
编写模板文件src/main/resouces/templates/hello.html:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
  xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1 th:inline="text">Hello.v.2</h1>
<p th:text="${hello}"></p>
</body>
</html>

(4)编写访问模板文件controller
@Controller
public class TemplateController {

/**
* 返回html模板.
*/
@RequestMapping("/helloHtml")
public String helloHtml(Map<String,Object> map){
map.put("hello","from TemplateController.helloHtml");
return "/helloHtml";
}

}

第14节 Spring Boot 使用freemarker
(1)在pom.xml中引入freemarker;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

(2)如何关闭freemarker缓存
########################################################
###FREEMARKER (FreeMarkerAutoConfiguration)
########################################################
spring.freemarker.allow-request-override=false
#开发过程关闭缓存
spring.freemarker.cache=false
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.prefix=
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
#spring.freemarker.suffix=.ftl
#spring.freemarker.template-loader-path=classpath:/templates/ #comma-separated list
#spring.freemarker.view-names= # whitelist of view names that can be resolved

(3)编写模板文件.ftl
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
  xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello.v.2</h1>
<p>${hello}</p>
</body>
</html>

(4)编写访问文件的controller
@RequestMapping("/helloFtl")
public String helloFtl(Map<String,Object> map){
map.put("hello","from TemplateController.helloFtl");
return "/helloFtl";
}

第15节 Spring Boot添加JSP支持
(1) 创建Maven web project;

(2) 在pom.xml文件添加依赖;
<!-- spring boot parent节点,引入这个之后,在下面和spring boot相关的就不需要引入版本了; -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>

<!-- 指定一下jdk的版本 ,这里我们使用jdk 1.8 ,默认是1.6 -->
    <java.version>1.8</java.version>
<!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- servlet 依赖. -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- JSTL(JSP Standard Tag Library,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库,是由apache的jakarta小组来维护的。-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

<!-- tomcat 的支持.-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>

(3) 配置application.properties支持jsp
添加src/main/resources/application.properties:

# 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/jsp/
# 响应页面默认后缀
spring.mvc.view.suffix=.jsp
# 自定义属性,可以在Controller中读取
application.hello=Hello Angel From application

(4) 编写测试Controller
@Controller
public class HelloController {
private String hello;   

@RequestMapping("/helloJsp")
public String helloJsp(Map<String,Object> map){
System.out.println("HelloController.helloJsp().hello=hello");
map.put("hello", hello);
return "helloJsp";
}
}

(5) 编写JSP页面
在 src/main 下面创建 webapp/WEB-INF/jsp 目录用来存放我们的jsp页面:helloJsp.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
helloJsp
<hr>
${hello}

</body>
</html>

(6) 编写启动类App.java
@SpringBootApplication
public class App {

public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
注意:
特别说明:针对el表达式,类似${hello} 这个对于servlet的版本是有限制的,2.4版本版本以下是不支持的,是无法进行识别的,请注意。


第16节 Spring Boot集成MyBatis

(1)新建maven project;

(2)在pom.xml文件中引入相关依赖;
(1)基本依赖,jdk版本号;
(2)mysql驱动,mybatis依赖包,mysql分页PageHelper:

<!-- mysql 数据库驱动. -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--
spring-boot mybatis依赖:

请不要使用1.0.0版本,因为还不支持拦截器插件,
1.1.1 是博主写帖子时候的版本,大家使用最新版本即可
-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.1.1</version>
</dependency>
<!--
    MyBatis提供了拦截器接口,我们可以实现自己的拦截器,
    将其作为一个plugin装入到SqlSessionFactory中。
Github上有位开发者写了一个分页插件,我觉得使用起来还可以,挺方便的。
Github项目地址: https://github.com/pagehelper/Mybatis-PageHelper
     -->
    <dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>4.1.0</version>
</dependency>

(3)创建启动类App.java
//这里和以往不一样的地方就是MapperScan的注解,这个是会扫描该包下的接口
@SpringBootApplication
@MapperScan("com.kfit.*")     //扫描该包下的class,主要是Mybatis的持久类
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

(4)在application.properties添加配置文件;
########################################################
###datasource
########################################################
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10

(5)编写Demo测试类;
public class Demo {
private long id;
private String name;
  //省略getter and setter….
}

(6)编写DemoMapper;
public interface DemoMappper {

@Select("select *from Demo where name = #{name}")
public List<Demo> likeName(String name);

@Select("select *from Demo where id = #{id}")
public Demo getById(long id);

@Select("select name from Demo where id = #{id}")
public String getNameById(long id);

@Insert("insert into Demo(name,password) values(#{name},#{password})")
public void save(Demo name);
}

(7)编写DemoService
@Service
public class DemoService {
@Autowired
private DemoMappper demoMappper;

public List<Demo> likeName(String name){
return demoMappper.likeName(name);
}
}

(8)编写DemoController;
@RestController
public class DemoController {
@Autowired
private DemoService demoService;

@RequestMapping("/likeName")
public List<Demo> likeName(String name){
return demoService.likeName(name);
}

}

//运行访问:http://127.0.0.1:8080/likeName?name=张三  就可以看到返回的数据了

(9)加入PageHelper
@Configuration
public class MyBatisConfiguration {

@Bean
public PageHelper pageHelper() {
System.out.println("MyBatisConfiguration.pageHelper()");
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
pageHelper.setProperties(p);
return pageHelper;
}
}
使用PageHelper分页
@RequestMapping("/likeName")
public List<Demo> likeName(String name){
PageHelper.startPage(1,1);
return demoService.likeName(name);
}
(10)获取自增长ID;

@Insert("insert into Demo(name,password) values(#{name},#{password})")
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
public long save(Demo name);

分享到:
评论

相关推荐

    SpringBoot学习笔记

    ### SpringBoot学习笔记 #### 一、SpringBoot入门 ##### 1、SpringBoot简介 - **简化Spring应用开发**:SpringBoot是简化Spring应用开发的一个框架,它通过默认配置极大地减少了开发过程中的样板代码,使得开发者...

    springboot学习笔记(适合学习使用,亲手打造)

    springboot学习笔记(适合学习使用,亲手打造),springboot学习笔记(适合学习使用,亲手打造),springboot学习笔记(适合学习使用,亲手打造),springboot学习笔记(适合学习使用,亲手打造),springboot学习...

    springboot学习笔记

    自己保存的springboot学习笔记,并非本人所写,只是觉得写的很好分享一下

    springboot学习笔记(史上最全)

    **SpringBoot学习笔记** SpringBoot是由Pivotal团队开发的一款基于Java的轻量级框架,旨在简化Spring应用的初始搭建以及开发过程。它通过自动配置、起步依赖和运行时嵌入式服务器,使得开发者能够快速地创建独立的...

    springboot学习笔记,适合新手

    1、首先对Spring Boot做一个简单的介绍。 2、开发工具idea,构建工具maven。 3、然后我们会创建一个简单的Spring Boot应用程序。 4、然后会们会遵循一个流行的系统架构风格 RESTful 来开发我们应用程序中的增删改查...

    SpringBoot学习笔记——尚硅谷

    SpringBoot学习笔记 尚硅谷 SpringBoot学习笔记 尚硅谷

    狂神springboot学习笔记.md

    狂神springboot学习笔记.md

    SpringBoot学习笔记所有源码集

    SpringBoot学习笔记所有源码集,包括 集成了freemarker、Servlet、拦截器、拦截器、Filter、静态资源处理、启动加载数据、Springboot 日志、JDBC使用、Springboot JPA、Mybatis、动态数据源、集成swagger、集成Redis...

    超详细的springboot学习笔记

    ### Spring Boot 学习笔记概览 #### 一、Spring 的发展历程 1. **Spring 1.x 时代:** - 在Spring框架发展的初期(即Spring 1.x时代),主要采用XML作为配置文件来管理Bean的声明周期。这种方式在项目规模较小的...

    springboot学习笔记源码

    这个"springboot学习笔记源码"包含了基于JSP、JPA和FreeMarker的实践项目,非常适合初学者或者希望深入理解SpringBoot集成这些技术的开发者进行学习。 1. **SpringBoot核心概念** SpringBoot的核心特性包括自动...

    springboot学习笔记day04.xmind

    springboot学习笔记day04.xmind

    springboot学习笔记day12.xmind

    springboot学习笔记day12.xmind

    springboot学习笔记day02.xmind

    springboot学习笔记day02.xmind

    springboot学习笔记day06.xmind

    springboot学习笔记day06.xmind

    SpringBoot学习笔记.rar

    SpringBoot学习笔记是一个压缩包,包含了对Spring Boot框架的详细学习资料。Spring Boot是Spring生态系统的简化启动器,旨在简化创建独立的、生产级别的基于Spring的应用程序。它通过提供默认配置来消除大量的XML...

    SpringBoot 学习笔记完整教程

    SpringBoot学习笔记完整教程 SpringBoot是Spring框架的一个简化版,旨在简化Spring应用程序的初始设置和开发过程。它集成了大量的常用库,如数据访问、安全、Web等,使得开发者可以快速构建健壮的微服务应用。本...

    springboot学习笔记day16.xmind

    springboot学习笔记day16.xmind

Global site tag (gtag.js) - Google Analytics