①在springboot中,dao层不定义方法,主要作用就是继承JpaRepository,在service层调用的不是dao层定义的方法,而是用dao层对象调用JpaRepository的方法,因为dao层对象和JpaRepository是继承关系。
②springdata操作数据库dao层是不需要实现类的
本案例创建的是IDEA的maven的web项目
<!--parent是继承,描述关系.体现maven的继承性-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.5.RELEASE</version>
<relativePath/>
</parent>
注意< dependency>标签放在< dependencies>标签中的。而 < parent>标签放在< dependencies>标签之上的
<!-- mvc,aop的依赖包。SpringBoot导进来的所有包都是通过这个依赖导进来的,体现maven的依赖性 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 由于我们在上面指定了parent,这里就不需要指定版本号 -->
</dependency>
<!--处理springboot的json数据中日期的依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
##链接数据库信息
spring.datasource.url=jdbc:mysql://localhost:3306/day06
spring.datasource.username=root
spring.datasource.password=root
##数据库连接驱动地址.这里试着手打,不然可能会报datasources错误
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
### spring jpa配置信息
##指定那个数据库
spring.jpa.database=MYSQL
##展示sql语句
spring.jpa.show-sql=true
##生成数据表
spring.jpa.hibernate.ddl-auto=update
##指定数据表的生成规则(即表名和类名相同)
spring.jpa.hibernate.naming-strateg=org.hibernate.cfg.ImprovedNamingStrategy
##方言,此处是mysql的数据库方言
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
将该实体类命名为:Users
package com.qf.entity;
import com.alibaba.fastjson.annotation.JSONField;
import javax.persistence.*;
import java.text.Format;
import java.util.Date;
@Entity
public class Users {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)//自增。sqlserver和mysql数据库用IDENTITY,Oracle数据库用.SEQUENCE
private Integer userId;
@Column// @Column(name = "username")//非主键列用Column,属性名与字段名分大小写,如果不一致要用@Column(name = "字段名")指定。
private String username;
@Column
@JSONField(format = "yyyy-MM-dd HH:mm:ss")//设置日期输出格式
private Date birthday;
@Column
private Integer age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Users{" +
"userId=" + userId +
", username='" + username + '\'' +
", birthday=" + birthday +
", age=" + age +
'}';
}
}
将该接口命名为:UsersDao
package com.qf.dao;
import com.qf.entity.Users;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UsersDao extends JpaRepository<Users,Integer>{//继承JpaRepository后记得加,不然会报UsersDao对象创建错误,Integer为实体类对应表的主键类型。因为是全查,所以没写方法
}
将接口定义为:UsersService
package com.qf.service;
import com.qf.entity.Users;
import java.util.List;
public interface UsersService {
public List<Users> getall();
}
将实现类定义为:UsersServiceImpl
package com.qf.service.impl;
import com.qf.dao.UsersDao;
import com.qf.entity.Users;
import com.qf.service.UsersService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UsersServiceImpl implements UsersService {
@Resource
UsersDao usersDao;
@Override
public List<Users> getall() {
return usersDao.findAll();//这个findAll()并不是我们定义的,是usersDao继承的JpaRepository类中的。也就是框架提供的
}
}
将该类命名为:TestController
package com.qf.controller;
import com.qf.entity.Users;
import com.qf.service.UsersService;
import org.apache.catalina.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
//RestController将方法返回结果转换成json格式并创建当前类对象。
@RestController//Res tController注解等价于@ResponseBody+@Controller;ResponseBody将方法返回结果转换成json格式,Controller用于创建当前类对象。
public class TestController {
@Resource
private UsersService usersService;
@RequestMapping("/getuserlist")
public List<Users> test3(HttpServletResponse response){
response.setContentType("text/html;charset=utf-8");
List<Users> list = usersService.getall();
System.out.println(list);
return list;//页面显示结果:[ { "age":12, "birthday":"2019-10-08 00:00:00", "userId":0, "username":"admin" } ]
}
}
将该类命名为:Test1
package com.qf;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import java.util.List;
@SpringBootApplication//声明这是springBoot的启动类。该启动类必须放在要启动的类(即含有@RestController)的父包里。不然会运行不成功
public class Test1 extends WebMvcConfigurerAdapter {
//重写configureMessageConverters,使实体类中的日期格式生效
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {//无返回值方法
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();//创建 FastJsonHttpMessageConverter对象
FastJsonConfig config = new FastJsonConfig();//创建FastJsonConfig对象
config.setSerializerFeatures(SerializerFeature.PrettyFormat);//设置格式的参数,SerializerFeature是一个枚举,里面有很多参数
converter.setFastJsonConfig(config);
converters.add(converter);
}
public static void main(String[] args) {
SpringApplication.run(Test1.class, args);//固定格式,Test1为本类类名。启动controller类,相当于SSM中启动Tomcat的步骤
}
}