IDEA14创建Maven管理的SpringMVC+Mybatis,web项目


项目构建步骤

1、File->New->Project

勾选Create from archetype

点击Next

2、输入GroupId、ArtifactId

点击Next

3、继续点击Next,输入Project name

点击Finish,完成基本项目创建

4、在src/main下添加java目录作为源文件目录

在main上右键new Directory并命名为java;

同时在Project Structure中,将java目录设置为Sources,然后Apply,点击OK。

5、配置pom.xml

可以在maven仓库进行相关依赖搜索:http://www.mvnrepository.com/

利用<dependency>元素进行项目所依赖的jar包配置,maven通过对pom.xml的配置使得不再需要导入jar包那么麻烦

6、配置tomcat


7、资源文件编译时一起打包到输出目录

    IDEA的maven项目中,默认源代码目录下的xml等资源文件并不会在编译的时候一块打包进classes文件夹,而是直接舍弃掉。

    项目中可能包含一些静态资源如:mapping/*.xml,又或者一些spring的配置文件:spring.xml、spring-mvc.xml等等。这些静态资源都要配置到target目录下才能保证项目运行,否则会报找不到运行所需的这些文件。

target目录设置,如下图:

pom.xml在resources中配置静态资源文件的目录:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
< resources >
     <!-- 该配置很重要,用于将directory下的资源复制到classpath下面。即target/classes下面-->
     < resource >
         < targetPath >whu/edu/irlab/mapping/</ targetPath >
         < directory >src/main/java/whu/edu/irlab/mapping/</ directory >
         < includes >
             < include >*.xml</ include >
         </ includes >
     </ resource >
     < resource >
         < directory >src/main/resources</ directory >
     </ resource >
</ resources >

在pom.xml配置后可以看见target目录如下,配置的静态资源文件被包含进来了



MavenDemo简单实例

1、项目结构


注:  在WEB-INFO/lib下存放有jstl.jar和standard.jar,需要将其导入项目的Libraries中(一般使用Maven不这样做,直接在pom.xml里面配置响应的jar包依赖就行,当时自动填补依赖有些问题故采取比较麻烦的方式做)

如图:


2、pom.xml配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<? 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/maven-v4_0_0.xsd" >
 
   < modelVersion >4.0.0</ modelVersion >
   < packaging >war</ packaging >
 
   < name >MavenDemo</ name >
   < groupId >MavenDemo</ groupId >
   < artifactId >MavenDemo</ artifactId >
   < version >1.0-SNAPSHOT</ version >
 
   < dependencies >
       <!-- spring start-->
       < dependency >
           < groupId >org.springframework</ groupId >
           < artifactId >spring-core</ artifactId >
           < version >4.1.6.RELEASE</ version >
       </ dependency >
 
       < dependency >
           < groupId >org.springframework</ groupId >
           < artifactId >spring-beans</ artifactId >
           < version >4.1.6.RELEASE</ version >
       </ dependency >
 
       < dependency >
           < groupId >org.springframework</ groupId >
           < artifactId >spring-context</ artifactId >
           < version >4.1.6.RELEASE</ version >
       </ dependency >
 
       < dependency >
           < groupId >org.springframework</ groupId >
           < artifactId >spring-web</ artifactId >
           < version >4.1.6.RELEASE</ version >
       </ dependency >
 
       < dependency >
           < groupId >org.springframework</ groupId >
           < artifactId >spring-webmvc</ artifactId >
           < version >4.1.6.RELEASE</ version >
       </ dependency >
 
       < dependency >
           < groupId >org.springframework</ groupId >
           < artifactId >spring-tx</ artifactId >
           < version >4.1.6.RELEASE</ version >
       </ dependency >
 
       < dependency >
           < groupId >org.springframework</ groupId >
           < artifactId >spring-context-support</ artifactId >
           < version >4.1.6.RELEASE</ version >
       </ dependency >
 
       < dependency >
           < groupId >org.springframework</ groupId >
           < artifactId >spring-jdbc</ artifactId >
           < version >4.1.6.RELEASE</ version >
       </ dependency >
       <!-- spring end-->
 
       <!-- mybatis start-->
       < dependency >
           < groupId >org.mybatis</ groupId >
           < artifactId >mybatis</ artifactId >
           < version >3.2.8</ version >
       </ dependency >
 
       < dependency >
           < groupId >org.mybatis</ groupId >
           < artifactId >mybatis-spring</ artifactId >
           < version >1.2.2</ version >
       </ dependency >
       <!-- mybatis end-->
 
       <!-- mysql connector start-->
       < dependency >
           < groupId >mysql</ groupId >
           < artifactId >mysql-connector-java</ artifactId >
           < version >5.1.34</ version >
       </ dependency >
       <!-- mysql connector end-->
 
       <!-- junit start-->
       < dependency >
           < groupId >junit</ groupId >
           < artifactId >junit</ artifactId >
           < version >4.11</ version >
       </ dependency >
       <!-- junit end-->
 
       <!-- 阿里巴巴数据源包 -->
       < dependency >
           < groupId >com.alibaba</ groupId >
           < artifactId >druid</ artifactId >
           < version >1.0.2</ version >
       </ dependency >
   </ dependencies >
 
</ project >

3、web.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<? xml  version = "1.0"  encoding = "UTF-8" ?>
< web-app  version = "2.4"
          xmlns = "http://java.sun.com/xml/ns/j2ee"
          xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
     < listener >
         < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class >
     </ listener >
 
     < listener >
         < listener-class >org.springframework.web.context.request.RequestContextListener</ listener-class >
     </ listener >
 
     < servlet >
         < servlet-name >dispatcher</ servlet-name >
         < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class >
         < load-on-startup >1</ load-on-startup >
     </ servlet >
     
     < servlet-mapping >
         < servlet-name >dispatcher</ servlet-name >
         < url-pattern >/</ url-pattern >
     </ servlet-mapping >
 
     < welcome-file-list >
         < welcome-file >/index.jsp</ welcome-file >
     </ welcome-file-list >
</ web-app >

4、applicationContext.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<? xml  version = "1.0"  encoding = "UTF-8" ?>
< beans  xmlns = "http://www.springframework.org/schema/beans"
        xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
                            ">
     < bean  id = "dataSource"  class = "com.alibaba.druid.pool.DruidDataSource"  init-method = "init"  destroy-method = "close" >
         < property  name = "driverClassName"  value = "com.mysql.jdbc.Driver"  />
         < property  name = "url"  value = "jdbc:mysql://localhost:3306/mybatis"  />
         < property  name = "username"  value = "root"  />
         < property  name = "password"  value = "root"  />
     </ bean >
 
     < bean  id = "sqlSessionFactory"  class = "org.mybatis.spring.SqlSessionFactoryBean" >
         < property  name = "dataSource"  ref = "dataSource"  />
     </ bean >
 
     < bean  id = "mapperScanner"  class = "org.mybatis.spring.mapper.MapperScannerConfigurer" >
         < property  name = "basePackage"  value = "mapper"  />
     </ bean >
</ beans >

5、dispathcer-servlet.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<? 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:mvc = "http://www.springframework.org/schema/mvc"
        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-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
 
     < mvc:default-servlet-handler  />
 
     < mvc:annotation-driven  />
 
     < context:component-scan  base-package = "controller"  />
     < context:component-scan  base-package = "mapper"  />
     < context:component-scan  base-package = "service"  />
 
     < bean  id = "viewResovler"  class = "org.springframework.web.servlet.view.InternalResourceViewResolver" >
         < property  name = "prefix"  value = "/WEB-INF/jsp/"  />
         < property  name = "suffix"  value = ".jsp"  />
     </ bean >
</ beans >

6、model/User.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package  model;
 
/**
  * Created by Roger on 2015/9/17.
  */
public  class  User {
     private  int  id;
     private  String name;
 
     public  int  getId() {
         return  id;
     }
 
     public  void  setId( int  id) {
         this .id = id;
     }
 
     public  String getName() {
         return  name;
     }
 
     public  void  setName(String name) {
         this .name = name;
     }
}

7、mapper/UserMapper.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package  mapper;
 
import  model.User;
import  org.apache.ibatis.annotations.Result;
import  org.apache.ibatis.annotations.Results;
import  org.apache.ibatis.annotations.Select;
import  org.springframework.stereotype.Repository;
 
import  java.util.List;
 
/**
  * Created by Roger on 2015/9/17.
  */
@Repository ( "userMapper" )
public  interface  UserMapper {
     @Select ( "select * from user" )
     @Results (value = { @Result (id =  true , property =  "id" , column =  "id" ),
                 @Result (property =  "name" , column =  "name" )})
     public  List<User> selectAll();
}

8、service/impl/UserServiceImpl.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package  service.impl;
 
import  mapper.UserMapper;
import  model.User;
import  org.springframework.stereotype.Service;
import  service.UserService;
 
import  javax.annotation.Resource;
import  java.util.List;
 
/**
  * Created by Roger on 2015/9/17.
  */
@Service ( "userService" )
public  class  UserServiceImpl  implements  UserService {
     @Resource (name =  "userMapper" )
     private  UserMapper userMapper;
 
     public  List<User> selectAll() {
         return  userMapper.selectAll();
     }
}

9、controller/UserController.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package  controller;
 
import  org.springframework.stereotype.Controller;
import  org.springframework.ui.Model;
import  org.springframework.web.bind.annotation.RequestMapping;
import  service.UserService;
 
import  javax.annotation.Resource;
 
/**
  * Created by Roger on 2015/9/17.
  */
@Controller
public  class  UserController {
     @Resource (name =  "userService" )
     private  UserService userService;
 
     @RequestMapping ( "/getUser" )
     public  String getUser(Model model) {
         System.out.println(userService.selectAll().size());
         model.addAttribute( "user" , userService.selectAll());
         return  "listUser" ;
     }
}

10、WEB-INFO/jsp/listUser.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%--
   Created by IntelliJ IDEA.
   User: Roger
   Date:  2015 / 9 / 17
   Time:  11 : 01
   To change  this  template use File | Settings | File Templates.
--%>
<%@ page contentType= "text/html;charset=UTF-8"  language= "java"  %>
<%@ taglib prefix= "c"  uri= "http://java.sun.com/jstl/core_rt"  %>
<html>
<head>
     <title>list user</title>
</head>
<body>
     <c:forEach items= "${user}"  var= "item" >
         ${item.id}--${item.name}
         <br>
     </c:forEach>
</body>
</html>

11、运行结果

通过getUser来访问数据库中user表格的数据

完成代码下载链接:http://pan.baidu.com/s/1qW01FmW


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值