1、spingIOC+IDEA+Maven

打印 上一主题 下一主题

主题 908|帖子 908|积分 2724

1、先配置spring、springmvc通用xml文件的头部信息
spring、springmvc通用xml文件的模板内容如下:
  1. 1 <?xml version="1.0" encoding="UTF-8"?>
  2. 2 <beans xmlns="http://www.springframework.org/schema/beans"
  3. 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. 4        xmlns:aop="http://www.springframework.org/schema/aop"
  5. 5        xmlns:context="http://www.springframework.org/schema/context"
  6. 6        xmlns:tx="http://www.springframework.org/schema/tx"
  7. 7        xmlns:mvc="http://www.springframework.org/schema/mvc"
  8. 8        xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. 9        http://www.springframework.org/schema/beans/spring-beans.xsd
  10. 10        http://www.springframework.org/schema/aop
  11. 11        http://www.springframework.org/schema/aop/spring-aop.xsd
  12. 12        http://www.springframework.org/schema/context
  13. 13        http://www.springframework.org/schema/context/spring-context.xsd
  14. 14        http://www.springframework.org/schema/tx
  15. 15        http://www.springframework.org/schema/tx/spring-tx.xsd
  16. 16        http://www.springframework.org/schema/mvc
  17. 17        http://www.springframework.org/schema/mvc/spring-mvc.xsd
  18. 18 ">
  19. 19   
  20. 20 </beans>
复制代码
spring、springmvc文件通用内容 

 

 
spring、spring
 
 
 
 
 
 
 
 
 2、构建spring的第一个工程

 
 
 
 
 
 
 

 
 
 
 
 
 3、配置项目中的pom.xml文件依赖
  1.   1 <?xml version="1.0" encoding="UTF-8"?>
  2.   2
  3.   3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.   4   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.   5   <modelVersion>4.0.0</modelVersion>
  6.   6
  7.   7   <groupId>com</groupId>
  8.   8   <artifactId>springmybatis_day44_02</artifactId>
  9.   9   <version>1.0-SNAPSHOT</version>
  10. 10   <packaging>war</packaging>
  11. 11
  12. 12   <name>springmybatis_day44_02 Maven Webapp</name>
  13. 13   
  14. 14   <url>http://www.example.com</url>
  15. 15
  16. 16   <properties>
  17. 17     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. 18     <maven.compiler.source>1.7</maven.compiler.source>
  19. 19     <maven.compiler.target>1.7</maven.compiler.target>
  20. 20   </properties>
  21. 21
  22. 22
  23. 23   <dependencies>
  24. 24
  25. 25     <dependency>
  26. 26       <groupId>junit</groupId>
  27. 27       <artifactId>junit</artifactId>
  28. 28       <version>4.11</version>
  29. 29     </dependency>
  30. 30
  31. 31     
  32. 32     <dependency>
  33. 33       <groupId>org.springframework</groupId>
  34. 34       <artifactId>spring-beans</artifactId>
  35. 35       <version>4.3.6.RELEASE</version>
  36. 36     </dependency>
  37. 37
  38. 38
  39. 39     
  40. 40     <dependency>
  41. 41       <groupId>org.springframework</groupId>
  42. 42       <artifactId>spring-context</artifactId>
  43. 43       <version>4.3.6.RELEASE</version>
  44. 44     </dependency>
  45. 45
  46. 46
  47. 47     
  48. 48     <dependency>
  49. 49       <groupId>org.springframework</groupId>
  50. 50       <artifactId>spring-core</artifactId>
  51. 51       <version>4.3.6.RELEASE</version>
  52. 52     </dependency>
  53. 53
  54. 54
  55. 55     
  56. 56     <dependency>
  57. 57       <groupId>org.springframework</groupId>
  58. 58       <artifactId>spring-expression</artifactId>
  59. 59       <version>4.3.6.RELEASE</version>
  60. 60     </dependency>
  61. 61
  62. 62
  63. 63     
  64. 64     <dependency>
  65. 65       <groupId>org.springframework</groupId>
  66. 66       <artifactId>spring-aop</artifactId>
  67. 67       <version>4.3.6.RELEASE</version>
  68. 68     </dependency>
  69. 69
  70. 70
  71. 71     
  72. 72     <dependency>
  73. 73       <groupId>aopalliance</groupId>
  74. 74       <artifactId>aopalliance</artifactId>
  75. 75       <version>1.0</version>
  76. 76     </dependency>
  77. 77
  78. 78
  79. 79     
  80. 80     <dependency>
  81. 81       <groupId>org.aspectj</groupId>
  82. 82       <artifactId>aspectjweaver</artifactId>
  83. 83       <version>1.8.10</version>
  84. 84     </dependency>
  85. 85
  86. 86
  87. 87     
  88. 88     <dependency>
  89. 89       <groupId>log4j</groupId>
  90. 90       <artifactId>log4j</artifactId>
  91. 91       <version>1.2.17</version>
  92. 92     </dependency>
  93. 93
  94. 94
  95. 95     
  96. 96     <dependency>
  97. 97       <groupId>commons-logging</groupId>
  98. 98       <artifactId>commons-logging</artifactId>
  99. 99       <version>1.2</version>
  100. 100     </dependency>
  101. 101
  102. 102
  103. 103     
  104. 104     <dependency>
  105. 105       <groupId>org.springframework</groupId>
  106. 106       <artifactId>spring-jdbc</artifactId>
  107. 107       <version>4.3.6.RELEASE</version>
  108. 108     </dependency>
  109. 109
  110. 110
  111. 111     
  112. 112     <dependency>
  113. 113       <groupId>org.springframework</groupId>
  114. 114       <artifactId>spring-orm</artifactId>
  115. 115       <version>4.3.6.RELEASE</version>
  116. 116     </dependency>
  117. 117
  118. 118
  119. 119     
  120. 120     <dependency>
  121. 121       <groupId>org.springframework</groupId>
  122. 122       <artifactId>spring-tx</artifactId>
  123. 123       <version>4.3.6.RELEASE</version>
  124. 124     </dependency>
  125. 125
  126. 126
  127. 127     
  128. 128     <dependency>
  129. 129       <groupId>org.springframework</groupId>
  130. 130       <artifactId>spring-web</artifactId>
  131. 131       <version>4.3.6.RELEASE</version>
  132. 132     </dependency>
  133. 133
  134. 134
  135. 135     
  136. 136     <dependency>
  137. 137       <groupId>org.springframework</groupId>
  138. 138       <artifactId>spring-webmvc</artifactId>
  139. 139       <version>4.3.6.RELEASE</version>
  140. 140     </dependency>
  141. 141
  142. 142
  143. 143     
  144. 144     <dependency>
  145. 145       <groupId>org.mybatis</groupId>
  146. 146       <artifactId>mybatis</artifactId>
  147. 147       <version>3.4.6</version>
  148. 148     </dependency>
  149. 149
  150. 150     
  151. 151     <dependency>
  152. 152       <groupId>org.mybatis</groupId>
  153. 153       <artifactId>mybatis-spring</artifactId>
  154. 154       <version>1.3.2</version>
  155. 155     </dependency>
  156. 156
  157. 157
  158. 158     
  159. 159     <dependency>
  160. 160       <groupId>mysql</groupId>
  161. 161       <artifactId>mysql-connector-java</artifactId>
  162. 162       <version>5.1.38</version>
  163. 163     </dependency>
  164. 164
  165. 165
  166. 166     
  167. 167     <dependency>
  168. 168       <groupId>javax.servlet</groupId>
  169. 169       <artifactId>javax.servlet-api</artifactId>
  170. 170       <version>3.1.0</version>
  171. 171     </dependency>
  172. 172
  173. 173     
  174. 174     <dependency>
  175. 175       <groupId>javax.servlet</groupId>
  176. 176       <artifactId>jstl</artifactId>
  177. 177       <version>1.2</version>
  178. 178     </dependency>
  179. 179
  180. 180
  181. 181     
  182. 182     <dependency>
  183. 183       <groupId>taglibs</groupId>
  184. 184       <artifactId>standard</artifactId>
  185. 185       <version>1.1.2</version>
  186. 186     </dependency>
  187. 187
  188. 188   </dependencies>
  189. 189
  190. 190
  191. 191 </project>
复制代码
pom.xml4、在项目的main下创建java和resouces目录


 
5、将java目录变为编译目录

 
 
6、将resources目录变为存放xml脚本的目录
 
 
 
 
 7、在java的com.pojo包下构建Student和Classes两个类 
  1. 1 package com.pojo;
  2. 2
  3. 3 public class Classes {
  4. 4     private Integer cid;
  5. 5     private String cname;
  6. 6
  7. 7     public Classes() {
  8. 8     }
  9. 9
  10. 10     public Classes(Integer cid, String cname) {
  11. 11         this.cid = cid;
  12. 12         this.cname = cname;
  13. 13     }
  14. 14
  15. 15     public Integer getCid() {
  16. 16         return cid;
  17. 17     }
  18. 18
  19. 19     public void setCid(Integer cid) {
  20. 20         this.cid = cid;
  21. 21     }
  22. 22
  23. 23     public String getCname() {
  24. 24         return cname;
  25. 25     }
  26. 26
  27. 27     public void setCname(String cname) {
  28. 28         this.cname = cname;
  29. 29     }
  30. 30
  31. 31     @Override
  32. 32     public String toString() {
  33. 33         return "Classes{" +
  34. 34                 "cid=" + cid +
  35. 35                 ", cname='" + cname + '\'' +
  36. 36                 '}';
  37. 37     }
  38. 38 }
复制代码
Classes.java
  1. 1 package com.pojo;
  2. 2
  3. 3 public class Student {
  4. 4     private Integer sid;
  5. 5     private String sname;
  6. 6     private String course;
  7. 7     private Double score;
  8. 8
  9. 9     private Classes classes;
  10. 10
  11. 11     public Student() {
  12. 12     }
  13. 13
  14. 14     public Student(Integer sid, String sname, String course, Double score, Classes classes) {
  15. 15         this.sid = sid;
  16. 16         this.sname = sname;
  17. 17         this.course = course;
  18. 18         this.score = score;
  19. 19         this.classes = classes;
  20. 20     }
  21. 21
  22. 22     public Integer getSid() {
  23. 23         return sid;
  24. 24     }
  25. 25
  26. 26     public void setSid(Integer sid) {
  27. 27         this.sid = sid;
  28. 28     }
  29. 29
  30. 30     public String getSname() {
  31. 31         return sname;
  32. 32     }
  33. 33
  34. 34     public void setSname(String sname) {
  35. 35         this.sname = sname;
  36. 36     }
  37. 37
  38. 38     public String getCourse() {
  39. 39         return course;
  40. 40     }
  41. 41
  42. 42     public void setCourse(String course) {
  43. 43         this.course = course;
  44. 44     }
  45. 45
  46. 46     public Double getScore() {
  47. 47         return score;
  48. 48     }
  49. 49
  50. 50     public void setScore(Double score) {
  51. 51         this.score = score;
  52. 52     }
  53. 53
  54. 54     public Classes getClasses() {
  55. 55         return classes;
  56. 56     }
  57. 57
  58. 58     public void setClasses(Classes classes) {
  59. 59         this.classes = classes;
  60. 60     }
  61. 61
  62. 62     @Override
  63. 63     public String toString() {
  64. 64         return "Student{" +
  65. 65                 "sid=" + sid +
  66. 66                 ", sname='" + sname + '\'' +
  67. 67                 ", course='" + course + '\'' +
  68. 68                 ", score=" + score +
  69. 69                 ", classes=" + classes +
  70. 70                 '}';
  71. 71     }
  72. 72 }
复制代码
Student.java8、在resouces下创建spring的applicationContext.xml文件
  1. 1 <?xml version="1.0" encoding="UTF-8"?>
  2. 2 <beans xmlns="http://www.springframework.org/schema/beans"
  3. 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. 4        xmlns:aop="http://www.springframework.org/schema/aop"
  5. 5        xmlns:context="http://www.springframework.org/schema/context"
  6. 6        xmlns:tx="http://www.springframework.org/schema/tx"
  7. 7        xmlns:mvc="http://www.springframework.org/schema/mvc"
  8. 8        xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. 9        http://www.springframework.org/schema/beans/spring-beans.xsd
  10. 10        http://www.springframework.org/schema/aop
  11. 11        http://www.springframework.org/schema/aop/spring-aop.xsd
  12. 12        http://www.springframework.org/schema/context
  13. 13        http://www.springframework.org/schema/context/spring-context.xsd
  14. 14        http://www.springframework.org/schema/tx
  15. 15        http://www.springframework.org/schema/tx/spring-tx.xsd
  16. 16        http://www.springframework.org/schema/mvc
  17. 17        http://www.springframework.org/schema/mvc/spring-mvc.xsd
  18. 18 ">
  19. 19      
  20. 20      
  21. 21      
  22. 22        
  23. 23        
  24. 24     
  25. 25     
  26. 26      <bean id="classes" class="com.pojo.Classes">
  27. 27           
  28. 28           
  29. 29           
  30. 30           
  31. 31           <property name="cid" value="1"/>
  32. 32           <property name="cname" value="教学部"/>
  33. 33      </bean>
  34. 34
  35. 35     <bean id="student" class="com.pojo.Student">
  36. 36          <property name="sid" value="10"/>
  37. 37          <property name="sname" value="holly"/>
  38. 38          <property name="course" value="java"/>
  39. 39          <property name="score" value="65.5"/>
  40. 40         
  41. 41          <property name="classes" ref="classes"/>
  42. 42     </bean>
  43. 43 </beans>
复制代码
applicationContext.xml9、在java的com.test包下构建StudentTest.java测试类 
  1. 1 package com.test;
  2. 2
  3. 3 import com.pojo.Classes;
  4. 4 import com.pojo.Student;
  5. 5 import org.junit.Test;
  6. 6 import org.springframework.context.ApplicationContext;
  7. 7 import org.springframework.context.support.ClassPathXmlApplicationContext;
  8. 8
  9. 9 //常规:需要对象时,主动创建,主动赋值,
  10. 10 //springIOC:需要对象时,被动接受完整对象(spring对我们所需要的对象进行赋值)
  11. 11 public class StudentTest {
  12. 12     @Test
  13. 13     public void normal(){
  14. 14         Classes classes=new Classes();
  15. 15         classes.setCid(1);
  16. 16         classes.setCname("教学部");
  17. 17
  18. 18         Student student=new Student();
  19. 19         student.setSid(10);
  20. 20         student.setSname("holly");
  21. 21         student.setCourse("java");
  22. 22         student.setScore(65.5);
  23. 23         student.setClasses(classes);
  24. 24
  25. 25         System.out.println(classes);
  26. 26         System.out.println(student);
  27. 27     }
  28. 28
  29. 29     @Test
  30. 30     public void springIOC(){
  31. 31         //加载spring的xml文件
  32. 32         ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
  33. 33
  34. 34         //spring给我们对象注入需要的值,我们根据bean 的id去查找赋值
  35. 35         Classes classes=(Classes) ac.getBean("classes");
  36. 36         Student student =(Student) ac.getBean("student");
  37. 37
  38. 38         System.out.println(classes);
  39. 39         System.out.println(student);
  40. 40     }
  41. 41 }
复制代码
StudentTest.java10、运行我们的测试方法且查看我们的打印效果
 
 
 
声明:此为原创,源码+理论讲解视频+操作视频,请私信作者!
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

惊雷无声

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表