Spring基于注解分层开发的案例实现
【摘要】 模拟一个User表的操作User类、控制层类、dao接口 及实现类 、serivice层接口 及实现类由于使用的maven的开发实现,所以需要先把maven的各项配置给处理好。第一步是增加spring的各项jar依赖。1.pom.xml文件的使用<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache....
模拟一个User表的操作
User类、控制层类、dao接口 及实现类 、serivice层接口 及实现类
由于使用的maven的开发实现,所以需要先把maven的各项配置给处理好。第一步是增加spring的各项jar依赖。
1.pom.xml文件的使用
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.aaa</groupId>
<artifactId>spring05_layer</artifactId>
<version>1.0-SNAPSHOT</version>
<!--依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.8</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
</dependencies>
</project>
下载完毕jar后,看下图所示:
2.在resources目录下建立spring.xml的配置文件,需要增加对目录的包扫描
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!--1.使用注解,启动包扫描 -->
<context:component-scan base-package="com.aaa"/>
</beans>
3.针对之前的项目操作,在java目录下建立像一个的包,分别是entity、dao、controller、service包,这些子包统一建立在com.aaa目录下。参考目录结构图如下所示:
3.1 entity下的User实体类如下
package com.aaa.entity;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User {
@Value("22")
private Integer id;
@Value("乔布斯")
private String name;
private Integer age;
private String hobby;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", hobby='" + hobby + '\'' +
'}';
}
}
3.2 dao包下的接口及其实现类
UserDao接口参考代码如下所示:
package com.aaa.dao;
import com.aaa.entity.User;
import java.util.List;
public interface IUserDao {
public void saveUser(User user);
public void updateUser(User user);
public void delUserById(Integer id);
public List<User> selAllUsers();
public List<User>seleUser(User user);
public User selUserByCondition(User user);
}
UserDao接口的UserDaoImpl参考代码如下所示:
package com.aaa.dao;
import com.aaa.entity.User;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository("userdao")
public class IUserDaoImpl implements IUserDao{
public void saveUser(User user) {
System.out.println("执行增加操作");
}
public void updateUser(User user) {
System.out.println("执行更新操作");
}
public void delUserById(Integer id) {
}
public List<User> selAllUsers() {
return null;
}
public List<User> seleUser(User user) {
return null;
}
public User selUserByCondition(User user) {
return null;
}
}
IUserService接口的代码参考如下,目前只实现第一个功能;
package com.aaa.service;
import com.aaa.entity.User;
import java.util.List;
public interface IUserService {
public void saveUser(User user);
public void updateUser(User user);
public void delUserById(Integer id);
public List<User> selAllUsers();
public List<User>seleUser(User user);
public User selUserByCondition(User user);
}
业务层接口的实现类IUserServiceImpl的参考代码如下所示:
package com.aaa.service;
import com.aaa.dao.IUserDao;
import com.aaa.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("userService")
public class IUserServiceImpl implements IUserService{
@Autowired
private IUserDao userDao;
public void saveUser(User user) {
System.out.println("业务层执行增加操作");
userDao.saveUser(user);
}
public void updateUser(User user) {
System.out.println("业务层执行更新操作");
}
public void delUserById(Integer id) {
}
public List<User> selAllUsers() {
return null;
}
public List<User> seleUser(User user) {
return null;
}
public User selUserByCondition(User user) {
return null;
}
}
UserController控制层参考代码如下所示:
package com.aaa.controller;
import com.aaa.dao.IUserDao;
import com.aaa.entity.User;
import com.aaa.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class UserController {
@Autowired
private IUserService userService;
public void saveUser(User user){
userService.saveUser(user);
System.out.println("信息页面保存成功!");
}
}
测试类Test参考代码如下所示:
import com.aaa.controller.UserController;
import com.aaa.entity.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test0501 {
@Test
public void test01(){
ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
UserController userController = ac.getBean("userController", UserController.class);
userController.saveUser(new User());
}
}
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)