您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
今天小编给大家分享一下Spring框架基于注解怎么开发CRUD的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
1. Maven坐标
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.0.RELEASE</version> </dependency> <!-- SpringJDBC jar包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.0.RELEASE</version> </dependency> <!-- spring整合junit jar包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.0.RELEASE</version> <scope>test</scope> </dependency> <!-- Lombok jar包 --> <dependency> <scope>compile</scope> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.16</version> </dependency> <!--单元测试jar包--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!-- mysql数据库jar包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> <scope>runtime</scope> </dependency>
2. 实体类
@Data @Component //等价于 <bean id="user" class="org.westos.pojo.User"/> public class User { private Integer id; private String username; private Date birthday; private String sex; private String address; }
3. Spring配置类
用于代替原来的SpringIOC
@Configuration @ComponentScan(value = "org.westos") @PropertySource("classpath:jdbcConfig.properties") public class MyConfig { @Value("${jdbc.driver}") private String driver; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; //注入数据源 @Bean("dataSource") public DataSource createDataSource() { DruidDataSource ds = new DruidDataSource(); ds.setDriverClassName(driver); ds.setUrl(url); ds.setUsername(username); ds.setPassword(password); return ds; } @Bean("jdbcTemplate") //注入JDBCtemplate模板 public JdbcTemplate createJdbcTemplate(DataSource dataSource){ return new JdbcTemplate(dataSource); } }
4. 业务层代码
接口
public interface UserService { //查询所有用户 List<User> findAll(); //根据id查询用户 User findUserById(Integer uid); //保存用户 void save(User user); //更新用户 void update(User user); //删除用户 void delete(Integer uid); }
实现类
@Service("userService") public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; //查询所有用户 public List<User> findAll() { List<User> list = userDao.findAll(); return list; } //根据id查询用户 public User findUserById(Integer uid) { User user = userDao.findUserById(uid); return user; } //保存用户 public void save(User user) { userDao.save(user); } //更新用户 public void update(User user) { userDao.update(user); } //删除用户 public void delete(Integer uid) { userDao.delete(uid); } }
5. 持久层代码
接口
public interface UserDao { //查询所有 List<User> findAll(); //根据id查询 User findUserById(Integer uid); //保存 void save(User user); //更新 void update(User user); //删除 void delete(Integer uid); }
实现类
@Repository("userDao") public class UserDaoImpl implements UserDao { @Autowired private JdbcTemplate jdbcTemplate; //查询所有 public List<User> findAll() { return jdbcTemplate.query("select * from user", new BeanPropertyRowMapper<User>(User.class)); } //根据id查询 public User findUserById(Integer uid) { return jdbcTemplate.queryForObject("select * from user where id=?", new BeanPropertyRowMapper<User>(User.class), uid); } //保存 public void save(User user) { jdbcTemplate.update("insert into user(username,birthday,sex,address) values (?,?,?,?)", user.getUsername(), user.getBirthday(), user.getSex(), user.getAddress()); } //更新 public void update(User user) { jdbcTemplate.update("update user set username=?,birthday=?,sex=?,address=? where id = ?", user.getUsername(), user.getBirthday(), user.getSex(), user.getAddress(), user.getId()); } //删除 public void delete(Integer uid) { jdbcTemplate.update("delete from user where id=?", uid); } }
6. jdbc配置文件
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis_test jdbc.username=root jdbc.password=123
7. 测试类代码
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = MyConfig.class) public class MyTest { @Autowired private UserService userService; @Autowired private User user; @Test public void findAllTest(){ List<User> list = userService.findAll(); for (User user : list) { System.out.println(user); } } @Test public void findUserByIdTest(){ User user = userService.findUserById(74); System.out.println(user); } @Test public void saveUserTest(){ user.setUsername("张三"); user.setBirthday(new Date()); user.setSex("女"); user.setAddress("西安市长安区"); userService.save(user); } @Test public void updateUserTest(){ user.setId(75); user.setUsername("李四"); user.setBirthday(new Date()); user.setSex("女"); user.setAddress("西安市雁塔"); userService.update(user); } @Test public void deleteUserTest(){ user.setId(75); userService.delete(user.getId()); } }
8. 工程目录
以上就是“Spring框架基于注解怎么开发CRUD”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。