SpringBoot文件结构
1.实体类Entity
**
继承Serializable
标记为jpa实体类
@Entity
@Table(name = “userinfo”)
userinfo 数据库对应的表名
注:无参构造与有参构造
ToString 用于测试
**
import javax.persistence.*;
import java.io.Serializable;
//标记为jpa实体类
@Entity
@Table(name = "userinfo")
public class Userinfo implements Serializable {
@Id //标记主键
@GeneratedValue(strategy = GenerationType.IDENTITY)//自动递增
private int id;
private String name;
private String password;
private long num;
private int age;
private String email;
//无参构造
public Userinfo(){}
//全参构造
public Userinfo(int id,String name, String password, long num, int age,String email) {
this.id = id;
this.name = name;
this.password = password;
this.num = num;
this.age = age;
this.email = email;
}
@Override
public String toString() {
return "Userinfo{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", num=" + num +
", age=" + age +
'}';
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getNum() {
return num;
}
public void setNum(long num) {
this.num = num;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2.DAO类
**@Repository //标记为数据库操作层 注解 增删改查都做了
继承 JpaRepository<对应实体类,主键类型>类
Spring Data JPA的方法命名规则
Spring Data JPA的方法命名规则-官方文档
@Query(“select u from Userinfo u where age>?1 and age<?2”) :自定义HQL语句[面向对象语句]
1,2是传入的参数位置
Userinfo 可以写成 com.bbdm.demomysql.Userinfo
原生SQL语句查询 nativeQuery = true 使用原生sql
@Query(value = “select * from Userinfo where age>?1 and age<?2”,nativeQuery = true)
@Transactional //事务提交
@Modifying //标记 update delete insert
**
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
//JpaRepository<对应实体类,主键类型>
@Repository //标记为数据库操作层 注解 增删改查都做了
public interface UserinfoDAO extends JpaRepository<Userinfo,Integer> {
@Transactional //事务提交
@Modifying //标记 update delete insert
@Query("update Userinfo set age=?1 where name=?2") //hql语句
public int myupdate(int age, String name);
//手动挡
//自定义HQL语句[面向对象语句]
@Query("select u from com.bbdm.demomysql.Userinfo u where age>?1 and age<?2")
public List<Userinfo> getUser(int age1,int age2);
//原生SQL语句查询 nativeQuery = true 使用原生sql
@Query(value = "select * from Userinfo where age>?1 and age<?2",nativeQuery = true)
public List<Userinfo> getUserBetweenAge(int age1,int age2);
//自动挡
//根据姓名查询
public List<Userinfo> findByName(String name);
//查询年龄大于20的用户信息
public List<Userinfo> findByAgeGreaterThan(int age);
//查询年龄大于20 并且学号为**
public List<Userinfo> findByAgeGreaterThanAndNumIs(int age,long num);
//根据名字模糊查询
public List<Userinfo> findByNameContaining(String name);
//根据mun查询并根据age降序
public List<Userinfo> findByNumOrderByAgeDesc(long num);
}
3.控制类Controller
import com.yihan.springbootcurd.dao.UserinfoDAO;
import com.yihan.springbootcurd.entity.Userinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller //处理请求
@RequestMapping("/user") //父路径
public class UserController {
@Autowired //依赖注入对象
UserinfoDAO userinfoDAO;
@RequestMapping("/list") //配置访问路径
public String list(Model model){
List<Userinfo> lists = userinfoDAO.findAll();
model.addAttribute("ulist", lists);//绑定参数
return "userList";//返回jsp页面
}
}