public interface TaskDao extends JpaRepository{
@Query("select t from Task t where t.taskName=?1")//1表示这是第一个参数
Task findByTaskName(String taskName);
}
除了写hql,我们还可以写sql语句
public interface TaskDao extends JpaRepository {
@Query("select * from tb_task t where t.task_name = ?1", nativeQuery = true)
Task findByTaskName(String taskName);
}
传参也可以这样传:
public interface TaskDao extends JpaRepository {
@Query("select t from Task t where t.taskName = ? and t.createTime = ?")
Task findByTaskName(String taskName, Date createTime);
}
还可以这样传:
public interface TaskDao extends JpaRepository {
@Query("select t from Task t where t.taskName = :taskName and t.createTime = :createTime")
Task findByTaskName(@Param("taskName")String taskName,@Param("createTime") Date createTime);
}
自定义查询语句+分页效果
/**
* 模糊查询
**/
@Query("from Document d where d.author like %?1% or content like %?1% or title like %?1%")
Page findByOneKey(String key,Pageable pageable);
//测试方法
Page documents=documentRepository.findByOneKey("haien",new PageRequest(0,10, Sort.Direction.DESC,"createDate"));
我们可以利用SqEL表达式,把实体类写成动态的:
public interface TaskDao extends JpaRepository {
@Query("select t from #{#entityName} t where t.taskName = ? and t.createTime = ?")
Task findByTaskName(String taskName, Date createTime);
}
- 其中#{#entityName}表示获取entityName的值,即实体类的名称。
- 实体类Task在使用@Entity注释后,Spring会将它纳入管理,若未指定表名,则entityName的值为Task,指定了则为指定值。
- Spring管理着多个实体类,谁调用了这个方法entityName就等于哪个实体类。
它的作用是,当两个实体类都有共同的父类时,可以抽取出一个通用repository
首先定义一个父类
// JPA 基类的标识
@MappedSuperclass //被注释的实体类不映射到数据库,但其子类会
@SuppressWarnings("serial") //抑制编译器的警告(比如不要飘波浪线、小灯泡),括号里的值表示警告类型;可以设为unchecked来逃避异常处理
public abstract class IdEntity implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
两个子类
@Entity
public class Task extends IdEntity{
}
@Entity
public class Project extends IdEntity{
}
然后有一个通用的接口:
@NoRepositoryBean //此接口无对应的数据库,不能暴露为repository
public interface GenericDao extends JpaRepository {
@Query("select t from #{#entityName} t where t.id= ?1") //entityName是个特有名词,传进来什么实体就用什么代替
public T findById(Long id);
}
//dao层
@Query("select u from User u where u.name=?1")
public List findByName(String name, Pageable pageable);
//service层调用
@RequestMapping(value = "/params", method= RequestMethod.GET)
@ResponseBody
public String getEntryByParams(@RequestParam(value = "name", defaultValue = "林志强") String name, @RequestParam(value = "page", defaultValue = "0") Integer page, @RequestParam(value = "size", defaultValue = "15") Integer size) {
Sort sort = new Sort(Sort.Direction.DESC, "id");
Pageable pageable = new PageRequest(page, size, sort);
Page pages=userDao.findByName(name,pageable);
Iterator it=pages.iterator();
while(it.hasNext()){
System.out.println("value:"+((User)it.next()).getId());
}
return "success...login....";
}
@Modifying标识自定义更新语句
@Modifying //表明是更新语句
@Transactional //一定要有事务,不是在这里就是在service层,反正那个会被直接调用那个就必须带事务
@Query("update Task t set t.taskName = ?1 where t.id = ?2")
int updateTask(String taskName, Long id);
@Modifying
@Transactional
@Query("delete from Score where model = ?1 and year = ?2")
void deleteByModelAndYear(String model,int year);
@Modifying
@Transactional
@Query(value = "insert into users(username,password,update_time,people_id) value(?1,SUBSTRING(md5(?2),1,16),?3,?4)",nativeQuery = true)
int saveUsers(String username, String password, Date update_time, int peopleId);
public interface PersonRepository extends BaseRepository {
}
TestController
@RestController
public class TestController {
@Resource
private PersonRepository personRepository;
@GetMapping(value = "/test")
public String test( ){
//订单表与用户表关联,通过用户ID查询该用户的所有订单,只获取订单编号和订单详情。
String sql="select o.no,o.detail from person as p inner join order as o
on o.personid=p.id and p.id= ? "
Integer userId=11;
Page orderLists = personRepository.findPageByParams(sql,
new PageRequest(1,10),userId);
return orderLists;
}
}
GetUrlParam:function GetUrlParam(param){
var reg = new RegExp("(^|&)"+ param +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if(r!=null
==================================================
1、打开PowerDesigner12,在菜单中按照如下方式进行操作
file->Reverse Engineer->DataBase
点击后,弹出 New Physical Data Model 的对话框
2、在General选项卡中
Model name:模板名字,自
网站配置是apache+tomcat,tomcat没有报错,apache报错是:
The proxy server received an invalid response from an upstream server. The proxy server could not handle the request GET /. Reason: Error reading fr
Free variable A free variable of an expression is a variable that’s used inside the expression but not defined inside the expression. For instance, in the function literal expression (x: Int) => (x
Part Ⅰ:
《阿甘正传》Forrest Gump经典中英文对白
Forrest: Hello! My names Forrest. Forrest Gump. You wanna Chocolate? I could eat about a million and a half othese. My momma always said life was like a box ochocol
Json在数据传输中很好用,原因是JSON 比 XML 更小、更快,更易解析。
在Java程序中,如何使用处理JSON,现在有很多工具可以处理,比较流行常用的是google的gson和alibaba的fastjson,具体使用如下:
1、读取json然后处理
class ReadJSON
{
public static void main(String[] args)