0、数据库表
CREATE DATABASE springboot; USE springboot; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键', `name` varchar(50) DEFAULT NULL COMMENT '姓名', `age` int(11) DEFAULT NULL COMMENT '年龄', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; INSERT INTO user values (1,"村雨遥",25); INSERT INTO user values (2,"张三",26); INSERT INTO user values (3,"李四",27);
1、
package com.cunyu.controller; import com.cunyu.pojo.User; import com.cunyu.service.UserService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; import java.util.List; /** * @author : cunyu * @version : 1.0 * @className : UserController * @date : 2020/7/29 16:22 * @description : UserController */ @RequestMapping("/user") @RestController public class UserController { private static final Logger logger = LoggerFactory.getLogger(UserController.class); private final UserService userService; public UserController(UserService userService) { this.userService = userService; } @GetMapping("/list") public List<User> getUsers() { logger.info("从数据库读取数据");//打印到console return userService.getList(); } }
2、
package com.cunyu.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.stereotype.Component; /** * @author : cunyu * @version : 1.0 * @className : Author * @date : 2020/7/29 16:20 * @description : User 实体类 */ @Component @Data @AllArgsConstructor @NoArgsConstructor public class User { private Long id; private String name; private Integer age; }
3
package com.cunyu.service; import com.cunyu.pojo.User; import java.util.List; /** * @author : cunyu * @version : 1.0 * @className : UserService * @date : 2020/7/29 21:12 * @description : TODO */ public interface UserService { /** * @param * @return * @description 获取所有数据列表 * @date 2020/7/29 21:57 * @author cunyu1943 * @version 1.0 */ public List<User> getList(); }
4
package com.cunyu.service.Impl; import com.cunyu.pojo.User; import com.cunyu.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Service; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; /** * @author : cunyu * @version : 1.0 * @className : UserServiceImpl * @date : 2020/7/29 21:13 * @description : TODO */ @Service public class UserServiceImpl implements UserService { private final JdbcTemplate jdbcTemplate; public UserServiceImpl(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @Override public List<User> getList() { String sql = "SELECT id, name, age FROM user"; return (List<User>) jdbcTemplate.query(sql, new RowMapper<User>() { @Override public User mapRow(ResultSet resultSet, int i) throws SQLException { User user = new User(); user.setId(resultSet.getLong("id")); user.setName(resultSet.getString("name")); user.setAge(resultSet.getInt("age")); return user; } }); } }
5
package com.cunyu; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class JdbcPracApplication { public static void main(String[] args) { SpringApplication.run(JdbcPracApplication.class, args); } }
访问
标签:JDBC,SpringBoot,springframework,实例,cunyu,org,import,public,user From: https://www.cnblogs.com/Kaelthas/p/17080124.html