SSM框架中的配置文件繁杂,此文帮大家疏通一下整体流程!
一 环境准备
1.1 创建maven工程
如图所示:
输入一下信息:
groupId:组织名称
artifactId:项目名称
便可进入项目中
1.2添加本次项目需要的jar包
jar包版本号
<spring.version>5.0.2.RELEASE</spring.version> <slf4j.version>1.6.6</slf4j.version> <log4j.version>1.2.12</log4j.version> <shiro.version>1.2.3</shiro.version> <mysql.version>5.1.6</mysql.version> <mybatis.version>3.4.5</mybatis.version>
对应的说明(坐标文件)
1 <dependency> 2 <groupId>junit</groupId> 3 <artifactId>junit</artifactId> 4 <version>4.11</version> 5 <scope>test</scope> 6 </dependency> 7 8 <!-- spring --> 9 <dependency> 10 <groupId>org.aspectj</groupId> 11 <artifactId>aspectjweaver</artifactId> 12 <version>1.6.8</version> 13 </dependency> 14 15 <dependency> 16 <groupId>org.springframework</groupId> 17 <artifactId>spring-aop</artifactId> 18 <version>${spring.version}</version> 19 </dependency> 20 21 <dependency> 22 <groupId>org.springframework</groupId> 23 <artifactId>spring-context</artifactId> 24 <version>${spring.version}</version> 25 </dependency> 26 27 <dependency> 28 <groupId>org.springframework</groupId> 29 <artifactId>spring-web</artifactId> 30 <version>${spring.version}</version> 31 </dependency> 32 33 <dependency> 34 <groupId>org.springframework</groupId> 35 <artifactId>spring-webmvc</artifactId> 36 <version>${spring.version}</version> 37 </dependency> 38 39 <dependency> 40 <groupId>org.springframework</groupId> 41 <artifactId>spring-test</artifactId> 42 <version>${spring.version}</version> 43 </dependency> 44 45 <dependency> 46 <groupId>org.springframework</groupId> 47 <artifactId>spring-tx</artifactId> 48 <version>${spring.version}</version> 49 </dependency> 50 51 <dependency> 52 <groupId>org.springframework</groupId> 53 <artifactId>spring-jdbc</artifactId> 54 <version>${spring.version}</version> 55 </dependency> 56 57 <dependency> 58 <groupId>junit</groupId> 59 <artifactId>junit</artifactId> 60 <version>4.12</version> 61 <scope>test</scope> 62 </dependency> 63 64 <dependency> 65 <groupId>mysql</groupId> 66 <artifactId>mysql-connector-java</artifactId> 67 <version>${mysql.version}</version> 68 </dependency> 69 70 <dependency> 71 <groupId>javax.servlet</groupId> 72 <artifactId>servlet-api</artifactId> 73 <version>2.5</version> 74 <scope>provided</scope> 75 </dependency> 76 77 <dependency> 78 <groupId>javax.servlet.jsp</groupId> 79 <artifactId>jsp-api</artifactId> 80 <version>2.0</version> 81 <scope>provided</scope> 82 </dependency> 83 84 <dependency> 85 <groupId>jstl</groupId> 86 <artifactId>jstl</artifactId> 87 <version>1.2</version> 88 </dependency> 89 90 <!-- log start --> 91 <dependency> 92 <groupId>log4j</groupId> 93 <artifactId>log4j</artifactId> 94 <version>${log4j.version}</version> 95 </dependency> 96 97 <dependency> 98 <groupId>org.slf4j</groupId> 99 <artifactId>slf4j-api</artifactId> 100 <version>${slf4j.version}</version> 101 </dependency> 102 103 <dependency> 104 <groupId>org.slf4j</groupId> 105 <artifactId>slf4j-log4j12</artifactId> 106 <version>${slf4j.version}</version> 107 </dependency> 108 <!-- log end --> 109 <dependency> 110 <groupId>org.mybatis</groupId> 111 <artifactId>mybatis</artifactId> 112 <version>${mybatis.version}</version> 113 </dependency> 114 115 <dependency> 116 <groupId>org.mybatis</groupId> 117 <artifactId>mybatis-spring</artifactId> 118 <version>1.3.0</version> 119 </dependency> 120 121 <dependency> 122 <groupId>c3p0</groupId> 123 <artifactId>c3p0</artifactId> 124 <version>0.9.1.2</version> 125 <type>jar</type> 126 <scope>compile</scope> 127 </dependency> 128 <dependency> 129 <groupId>org.junit.jupiter</groupId> 130 <artifactId>junit-jupiter-api</artifactId> 131 <version>RELEASE</version> 132 <scope>compile</scope> 133 </dependency> 134 <dependency> 135 <groupId>org.junit.jupiter</groupId> 136 <artifactId>junit-jupiter-api</artifactId> 137 <version>RELEASE</version> 138 <scope>compile</scope> 139 </dependency>
1.3 标记文件夹
需要注意,要用IDEA中的mark as将文件夹分别标注为源代码和配置文件
1.4 编写实体类
1 public class User { 2 private int id; 3 private String username; 4 private Date birthday; 5 private String sex; 6 private String address; 7 8 public int getId() { 9 return id; 10 } 11 12 public void setId(int id) { 13 this.id = id; 14 } 15 16 public String getUsername() { 17 return username; 18 } 19 20 public void setUsername(String username) { 21 this.username = username; 22 } 23 24 public Date getBirthday() { 25 return birthday; 26 } 27 28 public void setBirthday(Date birthday) { 29 this.birthday = birthday; 30 } 31 32 public String getSex() { 33 return sex; 34 } 35 36 public void setSex(String sex) { 37 this.sex = sex; 38 } 39 40 public String getAddress() { 41 return address; 42 } 43 44 public void setAddress(String address) { 45 this.address = address; 46 } 47 48 @Override 49 public String toString() { 50 return "User{" + 51 "id=" + id + 52 ", username=‘" + username + ‘\‘‘ + 53 ", birthday=" + birthday + 54 ", sex=‘" + sex + ‘\‘‘ + 55 ", address=‘" + address + ‘\‘‘ + 56 ‘}‘; 57 } 58 }
1.5 编写业务层接口和持久层接口
持久层接口
1 public interface IUserDao { 2 public List<User> findAll(); 3 }
业务层接口
1 public interface IUserService { 2 public List<User> findAll(); 3 }
二、Spring框架
2.1 编写spring配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/tx 10 http://www.springframework.org/schema/tx/spring-tx.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop.xsd 13 http://www.springframework.org/schema/context 14 http://www.springframework.org/schema/context/spring-context.xsd"> 15 <!-- 配置 spring 创建容器时要扫描的包 --> 16 <context:component-scan base-package="com.justnow"> 17 <!--制定扫包规则,不扫描@Controller 注解的 JAVA 类,其他的还是要扫描 --> 18 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 19 </context:component-scan> 20 </beans>
使用componet-scan,可以让Spring自动识别哪些类需要被装配程Bean,从而减少在xml中的配置
2.2 使用注解配置业务层和持久层
此时,需要实现类,实现注解配置
其中持久层
1 import java.util.List; 2 3 @Repository("userDao") 4 public class UserDaoImpl implements IUserDao { 5 @Override 6 public List<User> findAll() { 7 System.out.println("查询了所有账户"); 8 return null; 9 } 10 }
业务层
1 @Service("userService") 2 public class UserService implements IUserService { 3 4 @Autowired 5 private IUserDao userDao; 6 7 @Override 8 public List<User> findAll() { 9 return userDao.findAll(); 10 } 11 }
因为我们使用的是自动检测Bean的方式,所以要明确知道哪些类需要注册为spring Bean。此时,需要使用构造性(stereotype)来注解所标注的类
@Component:把资源让spring来管理,相当于在xml中配置了一个Bean
@Controller:一般用于表现层的注解
@Service:一般用于业务层的注解
@Repository:一般用于持久层的注解
后面三个是第一个的衍生注解,与@Commponent作用和属性是都一样,只不过是提供了更加明确的使用场景。
注解的value指定了Bean的id。如@Service("userService"),可以表示将该类注册为Bean,其中id为userService。如果没有指定value的话,该Bean的id为类名第一个字母小写
2.3 测试spring能否独立运行
1 public class Test01Spring { 2 public static void main(String[] args) { 3 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 4 IUserService userService = (IUserService) context.getBean("userService"); 5 userService.findAll(); 6 } 7 }
拿到配置文件,根据id值实例化类,执行findAll方法
执行的结果为:
说明,上述的配置过程无问题!
原文地址:https://www.cnblogs.com/justn0w/p/11389783.html
时间: 2024-10-01 07:08:58