SpringBoot+SpringDataJpa后台常用注解




SpringDataJpa中sqlite的时间格式必须为' 2017-03-23 09:10:29.100'  否则报错


com.controller
@RestController  表示该类是controller层 //@RestController注解相当于@ResponseBody + @Controller合在一起的作用。
@RequestMapping("/")  请求路径
@Autowired spring自动创建该对象的bean,生成对象
@ResponseBody  返回结果放入body数据区,返回的不是html页面,一般为json


com.service
@Service  表示该类是service层


com.repository
@Query("select t from UsersEntity t where t.userName=?1 and t.from = ?2") 
UsersEntity findByUserNameAndFrom(String userName,String from);
//执行findByUserNameAndFrom的方法的时候不会去调用SpringDataJpa的匹配规则,直接调用query后面的hql语句


@Query("select * from tb_task t where t.task_name = ?1", nativeQuery = true)
  Task findByTaskName(String taskName);
  //执行findByTaskName的方法的时候不会去调用SpringDataJpa的匹配规则,直接调用query后面的sql语句


com.entity
@Entity  表示该类是entity
@Table(name = "users") //表名
@Id  表示该属性对应的字段是主键
@GeneratedValue(strategy = GenerationType.AUTO) //主键生成策略
@Column(name = "u_id") //对应的字段名




@RequestMapping("/findByUserNameAndFrom/{userName}/{from}")
@ResponseBody
public String findByUserNameAndFrom(@PathVariable String userName, @PathVariable String from) {
return userService.findByUserNameAndFrom(userName, from);
}
//@PathVariable 使用RequestMapping路径中的参数

下面是SpringDataJpa方法名命名规则:

关键字方法命名sql where字句
AndfindByNameAndPwdwhere name= ? and pwd =?
OrfindByNameOrSexwhere name= ? or sex=?
Is,EqualsfindById,findByIdEqualswhere id= ?
BetweenfindByIdBetweenwhere id between ? and ?
LessThanfindByIdLessThanwhere id < ?
LessThanEqualsfindByIdLessThanEqualswhere id <= ?
GreaterThanfindByIdGreaterThanwhere id > ?
GreaterThanEqualsfindByIdGreaterThanEqualswhere id > = ?
AfterfindByIdAfterwhere id > ?
BeforefindByIdBeforewhere id < ?
IsNullfindByNameIsNullwhere name is null
isNotNull,NotNullfindByNameNotNullwhere name is not null
LikefindByNameLikewhere name like ?
NotLikefindByNameNotLikewhere name not like ?

StartingWith

findByNameStartingWithwhere name like '?%'
EndingWithfindByNameEndingWithwhere name like '%?'
ContainingfindByNameContainingwhere name like '%?%'
OrderByfindByIdOrderByXDescwhere id=? order by x desc
NotfindByNameNotwhere name <> ?
InfindByIdIn(Collection<?> c)where id in (?)
NotInfindByIdNotIn(Collection<?> c)where id not  in (?)
True

findByAaaTue

where aaa = true
FalsefindByAaaFalsewhere aaa = false
IgnoreCasefindByNameIgnoreCasewhere UPPER(name)=UPPER(?)



springboot测试:
@RunWith(SpringJUnit4ClassRunner.class)
// 指定我们SpringBoot工程的Application启动类
@SpringBootTest(classes = { Application.class })
// @SpringApplicationConfiguration(classes = Application.class)
// 由于是Web项目,Junit需要模拟ServletContext,因此我们需要给我们的测试类加上@WebAppConfiguration。
@WebAppConfiguration







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot应用中连接数据库,通常需要使用Spring Data JPA或者Spring JDBC等技术。其中,Spring Data JPA是一种基于JPA规范的ORM框架,可以通过注解方式配置实体类映射关系,大大简化了数据访问的开发。 在Vue项目中,可以使用axios等HTTP客户端库与后台进行数据交互。一般来说,后台会提供RESTful API接口供前端调用,前端通过发送HTTP请求来获取或者提交数据。 下面是一个简单的示例,演示了如何使用Spring Boot和Vue.js连接MySQL数据库: 1. 在pom.xml中添加MySQL驱动依赖: ``` <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency> ``` 2. 配置数据源和JPA 在application.properties或者application.yml文件中添加以下配置: ``` # 数据源配置 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 # JPA配置 spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect ``` 3. 创建实体类和DAO接口 在src/main/java目录下创建实体类和DAO接口: ``` // User.java @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private Integer age; // getter和setter省略 } // UserRepository.java @Repository public interface UserRepository extends JpaRepository<User, Long> { } ``` 4. 创建Controller 在src/main/java目录下创建UserController.java文件,编写RESTful API接口: ``` @RestController @RequestMapping("/api/user") public class UserController { @Autowired private UserRepository userRepository; @GetMapping("/{id}") public ResponseEntity<User> getUserById(@PathVariable Long id) { Optional<User> user = userRepository.findById(id); return user.map(u -> ResponseEntity.ok().body(u)) .orElse(ResponseEntity.notFound().build()); } @PostMapping("") public User createUser(@RequestBody User user) { return userRepository.save(user); } } ``` 5. 在Vue.js中调用API接口 在Vue.js中使用axios库发送HTTP请求,获取或者提交数据: ``` // 获取用户信息 axios.get('/api/user/1').then(response => { console.log(response.data); }); // 创建用户信息 axios.post('/api/user', { name: 'Tom', age: 20, }).then(response => { console.log(response.data); }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值