Spring+SpringMVC+MyBatis+jsp(idea版本)

1.构建maevn的web项目                                                                                                   

 

 

 

 

 

 

 

 

 

 

 

 

   到此我们的maven的web工程已经创建完成!

 

 

2. 如果我们的web项目需要jar文件,则我们要开始在pom.xml文件中配置我们本地的查找及下载jar文件的路径。这个项目中我们是ssm架构,我们就来配置ssm的pom,注意所有的资源配置,均写在<dependencies>根节点下

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5   <modelVersion>4.0.0</modelVersion>
  6 
  7   <groupId>cn.xh</groupId>
  8   <artifactId>xinhua</artifactId>
  9   <version>1.0-SNAPSHOT</version>
 10   <packaging>war</packaging>
 11 
 12   <name>xinhua Maven Webapp</name>
 13   <!-- FIXME change it to the project's website -->
 14   <url>http://www.example.com</url>
 15 
 16   <properties>
 17     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 18     <maven.compiler.source>1.7</maven.compiler.source>
 19     <maven.compiler.target>1.7</maven.compiler.target>
 20   </properties>
 21 
 22   <dependencies>
 23     <dependency>
 24       <groupId>junit</groupId>
 25       <artifactId>junit</artifactId>
 26       <version>4.12</version>
 27       <scope>test</scope>
 28     </dependency>
 29 
 30     <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
 31     <dependency>
 32       <groupId>mysql</groupId>
 33       <artifactId>mysql-connector-java</artifactId>
 34       <version>5.1.38</version>
 35     </dependency>
 36 
 37 
 38 
 39     <!--1 https://mvnrepository.com/artifact/org.springframework/spring-beans -->
 40     <dependency>
 41       <groupId>org.springframework</groupId>
 42       <artifactId>spring-beans</artifactId>
 43       <version>4.3.6.RELEASE</version>
 44     </dependency>
 45 
 46 
 47     <!--2 https://mvnrepository.com/artifact/org.springframework/spring-context -->
 48     <dependency>
 49       <groupId>org.springframework</groupId>
 50       <artifactId>spring-context</artifactId>
 51       <version>4.3.6.RELEASE</version>
 52     </dependency>
 53 
 54 
 55     <!-- 3https://mvnrepository.com/artifact/org.springframework/spring-core -->
 56     <dependency>
 57       <groupId>org.springframework</groupId>
 58       <artifactId>spring-core</artifactId>
 59       <version>4.3.6.RELEASE</version>
 60     </dependency>
 61 
 62 
 63     <!--4 https://mvnrepository.com/artifact/org.springframework/spring-expression -->
 64     <dependency>
 65       <groupId>org.springframework</groupId>
 66       <artifactId>spring-expression</artifactId>
 67       <version>4.3.6.RELEASE</version>
 68     </dependency>
 69 
 70 
 71     <!--5 https://mvnrepository.com/artifact/org.springframework/spring-aop -->
 72     <dependency>
 73       <groupId>org.springframework</groupId>
 74       <artifactId>spring-aop</artifactId>
 75       <version>4.3.6.RELEASE</version>
 76     </dependency>
 77 
 78 
 79     <!--6 https://mvnrepository.com/artifact/aopalliance/aopalliance -->
 80     <dependency>
 81       <groupId>aopalliance</groupId>
 82       <artifactId>aopalliance</artifactId>
 83       <version>1.0</version>
 84     </dependency>
 85 
 86 
 87     <!--7 https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
 88     <dependency>
 89       <groupId>org.aspectj</groupId>
 90       <artifactId>aspectjweaver</artifactId>
 91       <version>1.8.10</version>
 92     </dependency>
 93 
 94 
 95     <!--8 https://mvnrepository.com/artifact/log4j/log4j -->
 96     <dependency>
 97       <groupId>log4j</groupId>
 98       <artifactId>log4j</artifactId>
 99       <version>1.2.17</version>
100     </dependency>
101 
102 
103     <!--9 https://mvnrepository.com/artifact/commons-logging/commons-logging -->
104     <dependency>
105       <groupId>commons-logging</groupId>
106       <artifactId>commons-logging</artifactId>
107       <version>1.2</version>
108     </dependency>
109 
110 
111     <!--10 https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
112     <dependency>
113       <groupId>org.springframework</groupId>
114       <artifactId>spring-jdbc</artifactId>
115       <version>4.3.6.RELEASE</version>
116     </dependency>
117 
118 
119     <!--11 https://mvnrepository.com/artifact/org.springframework/spring-orm -->
120     <dependency>
121       <groupId>org.springframework</groupId>
122       <artifactId>spring-orm</artifactId>
123       <version>4.3.6.RELEASE</version>
124     </dependency>
125 
126 
127     <!--12 https://mvnrepository.com/artifact/org.springframework/spring-tx -->
128     <dependency>
129       <groupId>org.springframework</groupId>
130       <artifactId>spring-tx</artifactId>
131       <version>4.3.6.RELEASE</version>
132     </dependency>
133 
134 
135     <!--13 https://mvnrepository.com/artifact/org.springframework/spring-web -->
136     <dependency>
137       <groupId>org.springframework</groupId>
138       <artifactId>spring-web</artifactId>
139       <version>4.3.6.RELEASE</version>
140     </dependency>
141 
142 
143     <!--14 https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
144     <dependency>
145       <groupId>org.springframework</groupId>
146       <artifactId>spring-webmvc</artifactId>
147       <version>4.3.6.RELEASE</version>
148     </dependency>
149 
150 
151     <!--15 https://mvnrepository.com/artifact/org.mybatis/mybatis -->
152     <dependency>
153       <groupId>org.mybatis</groupId>
154       <artifactId>mybatis</artifactId>
155       <version>3.4.6</version>
156     </dependency>
157 
158 
159     <!--16 https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
160     <dependency>
161       <groupId>mysql</groupId>
162       <artifactId>mysql-connector-java</artifactId>
163       <version>5.1.38</version>
164     </dependency>
165 
166 
167     <!--17 https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
168     <dependency>
169       <groupId>org.mybatis</groupId>
170       <artifactId>mybatis-spring</artifactId>
171       <version>1.3.2</version>
172     </dependency>
173 
174     <!--18 https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
175     <dependency>
176       <groupId>javax.servlet</groupId>
177       <artifactId>javax.servlet-api</artifactId>
178       <version>3.1.0</version>
179     </dependency>
180 
181     <!--19 https://mvnrepository.com/artifact/javax.servlet/jstl -->
182     <dependency>
183       <groupId>javax.servlet</groupId>
184       <artifactId>jstl</artifactId>
185       <version>1.2</version>
186     </dependency>
187 
188 
189     <!--20 https://mvnrepository.com/artifact/taglibs/standard -->
190     <dependency>
191       <groupId>taglibs</groupId>
192       <artifactId>standard</artifactId>
193       <version>1.1.2</version>
194     </dependency>
195 
196     <!--文件上传使用的jar-->
197     <!--21 https://mvnrepository.com/artifact/commons-io/commons-io -->
198     <dependency>
199       <groupId>commons-io</groupId>
200       <artifactId>commons-io</artifactId>
201       <version>2.4</version>
202     </dependency>
203     <!--22-->
204     <dependency>
205       <groupId>commons-fileupload</groupId>
206       <artifactId>commons-fileupload</artifactId>
207       <version>1.3.3</version>
208     </dependency>
209 
210     <!--23 下载ajax封装数据json格式交互的jar-->
211     <dependency>
212       <groupId>com.fasterxml.jackson.core</groupId>
213       <artifactId>jackson-databind</artifactId>
214       <version>2.9.6</version>
215     </dependency>
216 
217     <dependency>
218       <groupId>com.fasterxml.jackson.core</groupId>
219       <artifactId>jackson-core</artifactId>
220       <version>2.9.6</version>
221     </dependency>
222 
223     <dependency>
224       <groupId>com.fasterxml.jackson.core</groupId>
225       <artifactId>jackson-annotations</artifactId>
226       <version>2.9.6</version>
227     </dependency>
228 
229     <!--使用4的版本否则springmvc的xml-->
230     <dependency>
231       <groupId>com.github.pagehelper</groupId>
232       <artifactId>pagehelper</artifactId>
233       <version>4.1.6</version>
234     </dependency>
235   </dependencies>
236 
237   <build>
238     <finalName>xinhua</finalName>
239     <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
240       <plugins>
241         <plugin>
242           <artifactId>maven-clean-plugin</artifactId>
243           <version>3.1.0</version>
244         </plugin>
245         <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
246         <plugin>
247           <artifactId>maven-resources-plugin</artifactId>
248           <version>3.0.2</version>
249         </plugin>
250         <plugin>
251           <artifactId>maven-compiler-plugin</artifactId>
252           <version>3.8.0</version>
253         </plugin>
254         <plugin>
255           <artifactId>maven-surefire-plugin</artifactId>
256           <version>2.22.1</version>
257         </plugin>
258         <plugin>
259           <artifactId>maven-war-plugin</artifactId>
260           <version>3.2.2</version>
261         </plugin>
262         <plugin>
263           <artifactId>maven-install-plugin</artifactId>
264           <version>2.5.2</version>
265         </plugin>
266         <plugin>
267           <artifactId>maven-deploy-plugin</artifactId>
268           <version>2.8.2</version>
269         </plugin>
270       </plugins>
271     </pluginManagement>
272   </build>
273 </project>
pom.xml

3.根据业务需求连接数据库,在数据库中编写sql脚本,插入数据,而我们有了idea则不需要再打开专门操作数据库脚本的一些外部工具,idea本身就具备链接各类数据库和数据库操作的功能,我们一起来看下

 

 

 

 

 

 

 

 

 

 

 

 4.在工程中构建java目录                                        

 

 

 

 

 

 5、在java目录下编写我们后台的java文件,但是要注意文件的新包必须在原来构建项目时的groupid的目录下

 

 

 

 

 

 6、编写实体类

 

 

 

 1 package cn.xh.pojo;
 2 
 3 /**
 4  * 对数据库usertb表的数据做映射封装
 5  */
 6 public class Usertb {
 7     private Integer userid ;
 8     private String username;
 9     private String userpwd;
10 
11     public Usertb() {
12     }
13 
14     public Usertb(Integer userid, String username, String userpwd) {
15         this.userid = userid;
16         this.username = username;
17         this.userpwd = userpwd;
18     }
19 
20     public Integer getUserid() {
21         return userid;
22     }
23 
24     public void setUserid(Integer userid) {
25         this.userid = userid;
26     }
27 
28     public String getUsername() {
29         return username;
30     }
31 
32     public void setUsername(String username) {
33         this.username = username;
34     }
35 
36     public String getUserpwd() {
37         return userpwd;
38     }
39 
40     public void setUserpwd(String userpwd) {
41         this.userpwd = userpwd;
42     }
43 
44     @Override
45     public String toString() {
46         return "Usertb{" +
47                 "userid=" + userid +
48                 ", username='" + username + '\'' +
49                 ", userpwd='" + userpwd + '\'' +
50                 '}';
51     }
52 }
Usertb.java

7.编写数据访问层映射接口

 

 

 

 1 package cn.xh.mapper;
 2 
 3 import cn.xh.pojo.Usertb;
 4 /**
 5  * 数据访问层映射接口
 6  */
 7 public interface UsertbMapper {
 8     /**
 9      * 根据条件查询用户对象信息
10      * @param usertb 封装查询条件的用户对象
11      * @return 返回查询到的用户对象
12      */
13     public Usertb findUsertb(Usertb usertb);
14 
15     /**
16      * 添加用户对象
17      * @param usertb 封装数据执行脚本时添加的数据库字段信息
18      * @return 返回数据完成插入操作后受影响的条数
19      */
20     Integer addUsertb(Usertb usertb);
21 }
UsertbMapper.java

8. 构建存放xml文件的resources文件夹,且编写映射xml文件

 

 

 

 

 9.将java文件夹和resources文件夹进行设置

 

 

 

 10.编写UsertbMapper.xml文件

 

posted @ 2026-09-07 09:14  红酒人生  阅读(11)  评论(0)    收藏  举报