楼主: Lisrelchen
1189 14

Spring Cookbook [推广有奖]

11
Reader's 发表于 2017-7-23 01:28:24 |只看作者 |坛友微信交流群
  1. Connecting to a database
  2. In this recipe, we will connect to a MySQL or PostgreSQL database from a Spring application.
  3. To connect to another database system, go to http://www.oxygenxml.com/database_
  4. drivers.html to find the relevant dependencies, driver class, and URL type.
  5. Getting ready
  6. You need a MySQL or PostgreSQL database up and running.
  7. How to do it…
  8. Here are the steps to connect from a Spring application to an existing database:
  9. 1. Add the Maven dependency for Spring JDBC in pom.xml:
  10. <dependency>
  11. <groupId>org.springframework</groupId>
  12. <artifactId>spring-jdbc</artifactId>
  13. <version>4.1.6.RELEASE</version>
  14. </dependency>
  15. 2. If you're using MySQL, add its Maven dependency in pom.xml:
  16. <dependency>
  17. <groupId>mysql</groupId>
  18. <artifactId>mysql-connector-java</artifactId>
  19. <version>5.1.35</version>
  20. </dependency>
  21. 3. If you're using PostgreSQL, add its Maven dependency in pom.xml:
  22. <dependency>
  23. <groupId>postgresql</groupId>
  24. <artifactId>postgresql</artifactId>
  25. <version>9.1-901-1.jdbc4</version>
  26. </dependency>
复制代码

使用道具

12
Reader's 发表于 2017-7-23 01:29:12 |只看作者 |坛友微信交流群
  1. Creating a DAO class
  2. In this recipe, we will create a DAO (data access object) class. A DAO class provides methods
  3. to save and retrieve objects from the database. It can be used from a controller.
  4. How to do it…
  5. Here are the steps to create a DAO class:
  6. 1. Create a class annotated with @Repository:
  7. @Repository
  8. public class UserDAO {
  9. 2. Add an autowired JdbcTemplate field to it:
  10. @Autowired
  11. private JdbcTemplate jdbcTemplate;
复制代码

使用道具

13
Reader's 发表于 2017-7-23 01:30:37 |只看作者 |坛友微信交流群
  1. Calling a DAO method from a controller class
  2. In this recipe, we'll see how to call a DAO method from a controller class.

  3. How to do it…
  4. Here are the steps to use a DAO method from a controller class:
  5. 1. In your controller class, add the DAO as an @Autowired field:
  6. @Controller
  7. public class UserController {
  8. @Autowired
  9. private UserDAO userDAO;
  10. 2. Use the DAO in any controller method:
  11. userDAO.add(user);
复制代码

使用道具

14
Reader's 发表于 2017-7-23 01:32:35 |只看作者 |坛友微信交流群
  1. Saving an object
  2. In this recipe, we will create a DAO method to save an object in the database; a row will be
  3. added to the corresponding database table.
  4. How to do it…
  5. Define an SQL insert query with question marks as placeholders for the actual row values. Use
  6. the update() method to execute the query using the actual values from the object:
  7. public void add(User user) {
  8. String sql = "insert into user (first_name, age) values (?, ?)";
  9. jdbcTemplate.update(sql, user.getFirstName(), user.getAge());
  10. }
复制代码

使用道具

15
Reader's 发表于 2017-7-23 01:34:24 |只看作者 |坛友微信交流群
  1. Retrieving an object
  2. In this recipe, we create a DAO method to retrieve a database row, which we will use to create
  3. an object.
  4. How to do it…
  5. Use an SQL select query and create an object from the result using RowMapper:
  6. 1. In the DAO class, add an inline class implementing RowMapper. This class defines
  7. how to generate a User object from a database row:
  8. private class UserMapper implements RowMapper<User> {
  9. public User mapRow(ResultSet row, int rowNum) throws
  10. SQLException {
  11. User user = new User();
  12. user.setId(row.getLong("id"));
  13. user.setFirstName(row.getString("first_name"));
  14. user.setAge(row.getInt("age"));

  15. return user;
  16. }
  17. }
  18. 2. Add a DAO method which will perform an SQL select query and use a UserMapper
  19. object to generate a User object:
  20. public User findById(Long id) {
  21. String sql = "select * from user where id=?";
  22. User user = jdbcTemplate.queryForObject(sql, new
  23. Object[]{id}, new UserMapper());
  24. return user;
  25. }
复制代码

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-25 08:23