<dependencies> ... <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>5.3.9</version> </dependency> ... </dependencies> properties spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=username spring.datasource.password=password spring.datasource.driver-class-name=com.mysql.jdbc.Driver <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="com.example" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="url" value="${spring.datasource.url}" /> <property name="username" value="${spring.datasource.username}" /> <property name="password" value="${spring.datasource.password}" /> <property name="driverClassName" value="${spring.datasource.driver-class-name}" /> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan" value="com.example.entity" /> <property name="jpaProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <tx:annotation-driven /> </beans> import javax.persistence.*; @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private int age; } public interface UserDao { User findById(Long id); List<User> findAll(); void save(User user); void update(User user); void delete(User user); } import org.springframework.stereotype.Repository; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import java.util.List; @Repository public class UserDaoImpl implements UserDao { @PersistenceContext private EntityManager entityManager; @Override public User findById(Long id) { return entityManager.find(User.class, id); } @Override public List<User> findAll() { String jpql = "SELECT u FROM User u"; return entityManager.createQuery(jpql, User.class).getResultList(); } @Override public void save(User user) { entityManager.persist(user); } @Override public void update(User user) { entityManager.merge(user); } @Override public void delete(User user) { entityManager.remove(user); } } import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller @RequestMapping("/users") public class UserController { @Autowired private UserDao userDao; @RequestMapping("/") @ResponseBody public String index() { List<User> users = userDao.findAll(); StringBuilder sb = new StringBuilder(); for (User user : users) { sb.append(user.getName()).append(", "); } return sb.toString(); } }


上一篇:
下一篇:
切换中文