您好,登錄后才能下訂單哦!
今天小編給大家分享一下Spring框架基于注解怎么開發(fā)CRUD的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來(lái)了解一下吧。
1. Maven坐標(biāo)
<!-- 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> <!--單元測(cè)試jar包--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!-- mysql數(shù)據(jù)庫(kù)jar包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> <scope>runtime</scope> </dependency>
2. 實(shí)體類
@Data @Component //等價(jià)于 <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配置類
用于代替原來(lái)的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; //注入數(shù)據(jù)源 @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. 業(yè)務(wù)層代碼
接口
public interface UserService { //查詢所有用戶 List<User> findAll(); //根據(jù)id查詢用戶 User findUserById(Integer uid); //保存用戶 void save(User user); //更新用戶 void update(User user); //刪除用戶 void delete(Integer uid); }
實(shí)現(xiàn)類
@Service("userService") public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; //查詢所有用戶 public List<User> findAll() { List<User> list = userDao.findAll(); return list; } //根據(jù)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(); //根據(jù)id查詢 User findUserById(Integer uid); //保存 void save(User user); //更新 void update(User user); //刪除 void delete(Integer uid); }
實(shí)現(xiàn)類
@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)); } //根據(jù)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. 測(cè)試類代碼
@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("西安市長(zhǎng)安區(qū)"); 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框架基于注解怎么開發(fā)CRUD”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。