源码数据库
链接:https://pan.baidu.com/s/15yRxsHfUbls-AHtOJngOhA
提取码:kzgy
4.0.0
bj.sh.gy
SpringdateJpa
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.4.1.RELEASE
UTF-8
1.8
junit
junit
3.8.1
test
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
runtime
com.alibaba
druid
1.0.29
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-test
test
junit
junit
org.springframework
spring-test
org.springframework.boot
spring-boot-test
package com.rj.bd.enity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name = "user")
@Entity
public class User {
@Id
private String id;
private int age;
private String name;
private int t_version;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getT_version() {
return t_version;
}
public void setT_version(int t_version) {
this.t_version = t_version;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", age=" + age +
", name='" + name + '\'' +
", t_version=" + t_version +
'}';
}
}
package com.rj.bd.Dao;
import com.rj.bd.enity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author LXY
* @desc
* @time 2022-10-16 16:12
*/
@Repository
public interface UserDao extends JpaRepository {
}
package com.rj.bd.Service;
import com.rj.bd.enity.User;
import java.util.List;
/**
* @author LXY
* @desc
* @time 2022-10-16 16:14
*/
public interface UserService {
Listqueryall();
}
package com.rj.bd.ServiceImple;
import com.rj.bd.Dao.UserDao;
import com.rj.bd.Service.UserService;
import com.rj.bd.enity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author LXY
* @desc
* @time 2022-10-16 16:15
*/
@Service
public class UserServiceImple implements UserService {
@Autowired
UserDao dao;
@Override
public List queryall() {
return dao.findAll();
}
}
package com.rj.bd.Test;
import com.rj.bd.Service.UserService;
import com.rj.bd.enity.User;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
/**
* @author LXY
* @desc
* @time 2022-10-16 16:10
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class Test {
@Autowired
UserService service;
@org.junit.Test
public void show(){
List queryall = service.queryall();
System.out.println(queryall);
}
}
##配置数据源
spring.datasource.url=jdbc:mysql://localhost:3306/hibernates?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#JPA的配置
##hibernate方言的配置
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
##控制台显示sql语句
spring.jpa.show-sql=true
##是否根据实体类来更新数据库表
spring.jpa.hibernate.ddl-auto=update
package com.rj.bd.Dao;
import com.rj.bd.enity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author LXY
* @desc
* @time 2022-10-16 16:12
*/
@Repository
public interface UserDao extends JpaRepository {
/**
* @Query:注解查询适用于所查询的数据无法通过关键字查询得到结果的查询。这种查询可以摆脱像关键字查询那样的约束,
将查询直接在相应的接口方法中声明,结构更为清晰,这是Spring Data的特有实现
1).当使用@Query时函数的名词就可以随便一点了
2).没有参数的查询
3).两个或者多个参数的耦合
4).使用@Param注解注入参数
5).模糊查询:like
6).@Query 注解支持使用百分号
7).@Query 使用原生的 SQL:如果我们使用原生的 SQL,控制台打印的语句也会是原生 SQL 的
友情提示:无论是关键字查询还是@Query查询,他们仅是对查询好使,其他不好使
*/
@Query(" select p from com.rj.bd.enity.User p where id=?1 ")
User selectont(String id);
@Query(" select p from com.rj.bd.enity.User p where id=:id ") // @Param注解: 注入/传入参数
User selectont2(@Param("id") String id);
/**
* nativeQuery=true:意思为使用本地查询,其实就是使用原生的sql语句
*
* 友情提示:据数据库的不同,在sql的书写语法方面可能有所区别
*
*/
@Query(value = " select * from user ",nativeQuery = true)
List slectqeuery();
}
@Modifying
@Query("delete com.rj.bd.enity.User u where u.id=?1")
int deleteuser(String id);
@Transactional
@Override
public int savauser() {
User user=new User();
user.setId(UUID.randomUUID().toString());
user.setName("猫咪");
user.setAge(15);
dao.save(user);
return 1;
}
@Transactional
@Override
public int savauser() {
Listlist=new ArrayList<>();
User user=new User();
user.setId(UUID.randomUUID().toString());
user.setName("猫咪123");
user.setAge(15);
User user1=new User();
user1.setId(UUID.randomUUID().toString());
user1.setName("猫咪123456");
user1.setAge(15);
list.add(user);
list.add(user1);
dao.save(list);
return 1;
}
User one = dao.findOne("1");
System.out.println(one);
List all = dao.findAll();
System.out.println(all);
Listlist=new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
List all = dao.findAll(list);
System.out.println(all);
long count = dao.count();
System.out.println(count);
User one = dao.findOne("1");
one.setName("xasd");
dao.delete("ba8069ec-3dac-49f7-aa28-695f18b095b2");
User one = dao.findOne("9dd9db37-7df5-48b2-a3b7-2d94052a62a3");
//两种方法都可 dao.delete(one);
deleteAll();方法删除全部
int page=1-1;
int size=2;
Pageable pageable=new PageRequest(page,2);
Page all = dao.findAll(pageable);
System.out.println("总记录数"+all.getTotalElements());
System.out.println("总页数"+all.getTotalPages());
System.out.println("当前时第几页"+all.getNumber()+1);
System.out.println("当前显示的条数"+all.getNumberOfElements());
List content = all.getContent();
System.out.println(content);
org.springframework.data.domain.Sort .Order order=new org.springframework.data.domain.Sort .Order(Sort.Direction.ASC,"age");
org.springframework.data.domain.Sort .Order order2=new org.springframework.data.domain.Sort .Order(Sort.Direction.DESC,"name");
Sort sort=new Sort(order,order2);
int page=1-1;
int size=2;
Pageable pageable=new PageRequest(page,2,sort);
Page all = dao.findAll(pageable);
System.out.println("总记录数"+all.getTotalElements());
System.out.println("总页数"+all.getTotalPages());
System.out.println("当前时第几页"+all.getNumber()+1);
System.out.println("当前显示的条数"+all.getNumberOfElements());
List content = all.getContent();
System.out.println(content);
Specification spec=new Specification() {
/**
*
* @param root: 意思为查询的实体类
* @param query :可以从中可到 Root 对象, 即告知 JPA Criteria 查询要查询哪一个实体类. 还可以
* 来添加查询条件, 还可以结合 EntityManager 对象得到最终查询的 TypedQuery 对象.
* @param cb: 用于创建Criteria 相关对象的工厂,且可以从中获取到Predicate
* @return Predicate 代表是一个查询条件
*/
@SuppressWarnings("rawtypes")
@Override
public Predicate toPredicate(Root root, CriteriaQuery query,
CriteriaBuilder cb) {
Path path = root.get("age");//获取Jpa_person表中age这个类的数据,且存入到Path对象中
Predicate predicate = cb.gt(path, 6);//创建一个条件查询:查询person对象对应的Jpa_Person表中的age这个列中的年龄大于50的数据
return predicate;//where __age___ > 60
}
};
org.springframework.data.domain.Sort .Order order=new org.springframework.data.domain.Sort .Order(Sort.Direction.ASC,"age");
org.springframework.data.domain.Sort .Order order2=new org.springframework.data.domain.Sort .Order(Sort.Direction.DESC,"name");
Sort sort=new Sort(order,order2);
int page=1-1;
int size=2;
Pageable pageable=new PageRequest(page,2,sort);
Page all = dao.findAll(spec,pageable);
System.out.println("总记录数"+all.getTotalElements());
System.out.println("总页数"+all.getTotalPages());
System.out.println("当前时第几页"+all.getNumber()+1);
System.out.println("当前显示的条数"+all.getNumberOfElements());
List content = all.getContent();
System.out.println(content);
return 1;
}
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.1
com.baomidou
mybatis-plus-boot-starter
3.4.3
mybatis-plus.type-aliases-package=com.rj.bd.enity
mybatis-plus.mapper-locations=classpath*:/mapper/**/*.xml
mybatis-plus.configuration.map-underscore-to-camel-case: true
mybatis-plus.configuration.cache-enabled: false
mybatis-plus.configuration.call-setters-on-nulls: true
com.alibaba
druid
1.2.6
@ConfigurationProperties("spring.datasource")
@Bean
public DataSource dataSource() throws SQLException {
DruidDataSource druidDataSource = new DruidDataSource();
//我们一般不建议将数据源属性硬编码到代码中,而应该在配置文件中进行配置(@ConfigurationProperties 绑定)
// druidDataSource.setUrl("jdbc:mysql://127.0.0.1:3306/bianchengbang_jdbc");
// druidDataSource.setUsername("root");
// druidDataSource.setPassword("root");
// druidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
return druidDataSource;
}
测试:
@Autowired
DataSource dataSource;
@Autowired
JdbcTemplate jdbcTemplate;
@org.junit.Test
public void contextLoads() throws SQLException {
System.out.println("默认数据源为:" + dataSource.getClass());
System.out.println("数据库连接实例:" + dataSource.getConnection());
//访问数据库
Integer i = jdbcTemplate.queryForObject("SELECT count(*) from `user`", Integer.class);
System.out.println("user 表中共有" + i + "条数据。");
}