1.pom新增依賴
<!-- spring data jpa,会注入tomcat jdbc pool/hibernate等 --> org.springframework.bootspring-boot-starter-data-jpamysqlmysql-connector-java5.1.42
#2.新增資料來源配置(DataSource啥的,一系列物件spring boot 都會給你注入的,配置配置即可!層
spring.datasource.url=jdbc:mysql:spring.datasource.username==123456--name=-wait=10000-active=300-on-borrow=-size=20
#=====================jpa config================================ #实体类维护数据库表结构的具体行为:update/create/create-drop/validate/none spring.jpa.hibernate.ddl-auto=none #打印sql语句 spring.jpa.show-sql=true #格式化输出的json字符串 spring.jackson.serialization.indent_output=true
5.service層
@Entity @Table(name="user")public class User { @Id @Column(name="id") @GeneratedValue(strategy = GenerationType.IDENTITY)private Integer id; @Column(name="number")private String number; @Column(name="name")private String name;public Integer getId() {return id; }public void setId(Integer id) {this.id = id; }public String getNumber() {return number; }public void setNumber(String number) {this.number = number; }public String getName() {return name; }public void setName(String name) {this.name = name; } }
#
public interface UserDao{ User getById(int id); User getByNumber(String number);int addUser(User user);void deleteUserById(int id); User updateUser(User user); }
6.controller層
@Repositorypublic class UserDaoImpl implements UserDao { @PersistenceContextprivate EntityManager entityManager; @Overridepublic User getById(int id) {//find by primary keyreturn this.entityManager.find(User.class,id); } @Overridepublic User getByNumber(String number) { Query query = this.entityManager.createQuery("from User u where u.number=:number",User.class); query.setParameter("number",number); User user = (User)query.getSingleResult();return user; } @Overridepublic int addUser(User user) {this.entityManager.persist(user);//print the id System.out.println(user.getId());return user.getId(); } @Overridepublic void deleteUserById(int id) { User user = this.entityManager.find(User.class,id); //关联到记录,方可删除this.entityManager.remove(user); } @Overridepublic User updateUser(User user) { User userNew = this.entityManager.merge(user);return userNew; } }
public interface UserService { User getById(int id); User getByNumber(String number);int addUser(User user,boolean throwEx);void deleteUserById(int id); User updateUser(User user); }
@Service @Transactionalpublic class UserServiceImpl implements UserService { @Autowiredprivate UserDao userDao; @Override @Transactional(readOnly = true)public User getById(int id) {return userDao.getById(id); } @Override @Transactional(readOnly = true)public User getByNumber(String number) {return userDao.getByNumber(number); } @Overridepublic int addUser(User user,boolean throwEx) {int id= this.userDao.addUser(user);if(throwEx){throw new RuntimeException("throw a ex"); }return id; } @Overridepublic void deleteUserById(int id) {this.userDao.deleteUserById(id); } @Overridepublic User updateUser(User user) {return this.userDao.updateUser(user); } }
以上是spring boot 1.5.4 整合 jpa+hibernate+jdbcTemplate的詳細內容。更多資訊請關注PHP中文網其他相關文章!