码迷,mamicode.com
首页 > 其他好文 > 详细

maven+SSM框架工程搭建

时间:2017-04-25 17:00:38      阅读:339      评论:0      收藏:0      [点我收藏+]

标签:--   html4   artifact   仓库   conf   语句   lap   1.0   ems   

1.百度下载 maven 和 tomcat 安装 配置环境变量

技术分享

2.使用最新版eclipse 集成maven

技术分享技术分享

maven conf文件夹下的settings.xml文件配置存放maven仓库的位置,D:\hongzhimei\repository 为存放路径

技术分享

3.新建工程

技术分享技术分享

项目原型选择webapp项目

 技术分享技术分享

 在buildPath中选择Edit更改为工作空间默认的jdk,项目目录会变成这样

技术分享

在程序部署中把test删除,程序就不会编译test的代码

技术分享

 在pom.xml文件中添加依赖,然后等待jar包下载.

技术分享
  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3   <modelVersion>4.0.0</modelVersion>
  4   <groupId>com.maven.ssm</groupId>
  5   <artifactId>weiyi</artifactId>
  6   <packaging>war</packaging>
  7   <version>0.0.1-SNAPSHOT</version>
  8   <name>weiyi Maven Webapp</name>
  9   <url>http://maven.apache.org</url>
 10   <properties>
 11         <!-- spring版本号 -->
 12         <spring.version>4.0.2.RELEASE</spring.version>
 13     </properties>
 14   <dependencies>
 15     <!-- spring核心包 -->
 16         <dependency>
 17             <groupId>org.springframework</groupId>
 18             <artifactId>spring-core</artifactId>
 19             <version>${spring.version}</version>
 20         </dependency>
 21 
 22         <dependency>
 23             <groupId>org.springframework</groupId>
 24             <artifactId>spring-web</artifactId>
 25             <version>${spring.version}</version>
 26         </dependency>
 27         <dependency>
 28             <groupId>org.springframework</groupId>
 29             <artifactId>spring-oxm</artifactId>
 30             <version>${spring.version}</version>
 31         </dependency>
 32         <dependency>
 33             <groupId>org.springframework</groupId>
 34             <artifactId>spring-tx</artifactId>
 35             <version>${spring.version}</version>
 36         </dependency>
 37 
 38         <dependency>
 39             <groupId>org.springframework</groupId>
 40             <artifactId>spring-jdbc</artifactId>
 41             <version>${spring.version}</version>
 42         </dependency>
 43 
 44         <dependency>
 45             <groupId>org.springframework</groupId>
 46             <artifactId>spring-webmvc</artifactId>
 47             <version>${spring.version}</version>
 48         </dependency>
 49         <dependency>
 50             <groupId>org.springframework</groupId>
 51             <artifactId>spring-aop</artifactId>
 52             <version>${spring.version}</version>
 53         </dependency>
 54 
 55         <dependency>
 56             <groupId>org.springframework</groupId>
 57             <artifactId>spring-context-support</artifactId>
 58             <version>${spring.version}</version>
 59         </dependency>
 60 
 61         <dependency>
 62             <groupId>org.springframework</groupId>
 63             <artifactId>spring-test</artifactId>
 64             <version>${spring.version}</version>
 65         </dependency>
 66         <!-- 添加MyBatis依赖 -->
 67         <dependency>
 68             <groupId>org.mybatis</groupId>
 69             <artifactId>mybatis</artifactId>
 70             <version>3.3.0</version>
 71         </dependency>
 72         <dependency>
 73             <groupId>mysql</groupId>
 74             <artifactId>mysql-connector-java</artifactId>
 75             <version>5.1.25</version>
 76         </dependency>
 77         <!-- 阿里巴巴的数据库连接池 -->
 78         <dependency>
 79             <groupId>com.alibaba</groupId>
 80             <artifactId>druid</artifactId>
 81             <version>1.0.16</version>
 82         </dependency>
 83         <!-- spring结成mybatis -->
 84         <dependency>
 85             <groupId>org.mybatis</groupId>
 86             <artifactId>mybatis-spring</artifactId>
 87             <version>1.2.3</version>
 88         </dependency>
 89         <!-- jsp标准标签库 -->
 90         <dependency>
 91             <groupId>javax.servlet</groupId>
 92             <artifactId>jstl</artifactId>
 93             <version>1.2</version>
 94         </dependency>
 95         <dependency>
 96             <groupId>log4j</groupId>
 97             <artifactId>log4j</artifactId>
 98             <version>1.2.16</version>
 99         </dependency>
100         <dependency>
101             <groupId>org.slf4j</groupId>
102             <artifactId>slf4j-api</artifactId>
103             <version>1.6.1</version>
104         </dependency>
105         <dependency>
106             <groupId>org.slf4j</groupId>
107             <artifactId>slf4j-nop</artifactId>
108             <version>1.6.4</version>
109         </dependency>
110         <dependency>
111             <groupId>junit</groupId>
112             <artifactId>junit</artifactId>
113             <version>4.7</version>
114             <scope>test</scope>
115         </dependency>
116   </dependencies>
117   <build>
118       <plugins>
119         <plugin>
120             <groupId>org.apache.tomcat.maven</groupId>
121             <artifactId>tomcat6-maven-plugin</artifactId>
122             <version>2.1</version>
123             <configuration>
124                 <url>http://localhost:8090/manager</url>
125                 <server>Tomcat6</server>
126                 <port>8090</port>
127                 <path>/</path>
128             </configuration>
129         </plugin>      
130       </plugins>
131     <finalName>weiyi</finalName>
132   </build>
133 </project>
pom.xml

目前这里会有个报错,需要配置一下servlet,我这里用的是Tomcat6

技术分享

技术分享技术分享

这样就不报错了。

4.数据库搭建

我这里就用了mysql,插入了一张很简单的表。

技术分享

5.整合SSM框架

搭建目录

技术分享

在resource文件夹下加上这些配置文件

技术分享

6.运行测试

显示结果技术分享

 

下面是jsp java代码

技术分享
 1 package com.weiyi.controller;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.ui.ModelMap;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 
 8 import com.weiyi.service.UserService;
 9 
10 @Controller
11 @RequestMapping(value="user")
12 public class UserController {
13     @Autowired
14     UserService service;
15     @RequestMapping(value="showInfo")
16     public String getUsers(ModelMap map){
17         map.addAttribute("users",service.userList());
18         System.out.println(map.get("users"));
19         return "userInfo";
20     }
21 }
UserController
技术分享
package com.weiyi.dao;

import java.util.List;

import com.weiyi.entity.User;

public interface UserDao {
    List<User> queryAll();
}
UserDao
技术分享
 1 package com.weiyi.entity;
 2 
 3 
 4 public class User {
 5     private Integer id;
 6     private String username;
 7     private String password;
 8     public User() {
 9         // TODO Auto-generated constructor stub
10     }
11     public User(Integer id, String username, String password) {
12         super();
13         this.id = id;
14         this.username = username;
15         this.password = password;
16     }
17     public Integer getId() {
18         return id;
19     }
20     public void setId(Integer id) {
21         this.id = id;
22     }
23     public String getUsername() {
24         return username;
25     }
26     public void setUsername(String username) {
27         this.username = username;
28     }
29     public String getPassword() {
30         return password;
31     }
32     public void setPassword(String password) {
33         this.password = password;
34     }
35     @Override
36     public String toString() {
37         return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
38     }
39     
40 }
User
技术分享
1 package com.weiyi.service;
2 
3 import java.util.List;
4 
5 import com.weiyi.entity.User;
6 
7 public interface UserService {
8     List<User> userList();
9 }
UserService
技术分享
 1 package com.weiyi.service.impl;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Service;
 7 
 8 import com.weiyi.dao.UserDao;
 9 import com.weiyi.entity.User;
10 import com.weiyi.service.UserService;
11 @Service
12 public class UserServiceImpl implements UserService {
13     @Autowired
14     UserDao dao;
15     
16     public List<User> userList() {
17         return dao.queryAll();
18     }
19 
20 }
UserServiceImpl
技术分享
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<h2>Hello World!</h2>
<a href="/user/showInfo"><button>进入用户库</button></a>
</body>
</html>
index.jsp
技术分享
 1 <%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%>
 2 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <c:forEach var="user" items="${users}">
11     姓名:${user.username} 密码:${user.password}
12     </c:forEach>
13 </body>
14 </html>
userInfo.jsp

下面是配置文件

技术分享
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE configuration  
 3   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
 4   "http://mybatis.org/dtd/mybatis-3-config.dtd">  
 5       
 6 <configuration>  
 7     <!-- 全局别名设置,在映射文件中只需写别名,而不必写出整个类路径  -->  
 8     <typeAliases>    
 9          <typeAlias type="com.weiyi.entity.User" alias="User"/>  
10     </typeAliases>     
11       <mappers>  
12         <mapper resource="com/weiyi/mapping/UserMapper.xml" />  
13     </mappers>
14 </configuration>
mybatis-config.xml
技术分享
1 log4j.rootLogger=DEBUG, stdout
2 log4j.logger.org.mybatis=DEBUG
3 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
4 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
5 log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n
log4j
技术分享
 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" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6      http://www.springframework.org/schema/beans/spring-beans.xsd
 7      http://www.springframework.org/schema/context
 8      http://www.springframework.org/schema/context/spring-context.xsd
 9      http://www.springframework.org/schema/aop
10      http://www.springframework.org/schema/aop/spring-aop.xsd
11      http://www.springframework.org/schema/tx 
12      http://www.springframework.org/schema/tx/spring-tx.xsd">
13      
14      <!-- 配置数据源 -->
15     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
16         <!-- 基本属性 url、user、password -->  
17         <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
18         <property name="url" value="jdbc:mysql://127.0.0.1:3306/student" />  
19         <property name="username" value="root" />  
20         <property name="password" value="aabbcc" />  
21         <property name="initialSize" value="1" />  
22         <property name="minIdle" value="1" />   
23         <property name="maxActive" value="20" />  
24         <property name="maxWait" value="60000" />
25         <!-- 超过时间限制是否回收 -->
26         <property name="removeAbandoned" value="true" />
27         <!-- 超过时间限制多长; -->
28         <property name="removeAbandonedTimeout" value="180" />
29         <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
30         <property name="timeBetweenEvictionRunsMillis" value="60000" />
31         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
32         <property name="minEvictableIdleTimeMillis" value="300000" />
33         <!-- 用来检测连接是否有效的sql,要求是一个查询语句-->
34         <property name="validationQuery" value="SELECT 1" />
35         <!-- 申请连接的时候检测 -->
36         <property name="testWhileIdle" value="true" />
37         <!-- 申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能 -->
38         <property name="testOnBorrow" value="false" />
39         <!-- 归还连接时执行validationQuery检测连接是否有效,配置为true会降低性能  -->
40         <property name="testOnReturn" value="false" />
41     </bean>
42      
43     <!-- Mybatis文件 -->
44     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
45         <property name="configLocation" value="classpath:mybatis-config.xml" /> 
46         <property name="dataSource" ref="dataSource" />
47         <!-- 映射文件路径 -->
48         <!--  <property name="mapperLocations" value="com/weiyi/mapping/*.xml" />-->
49     </bean>
50      
51      <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
52         <property name="basePackage" value="com.weiyi.dao" />
53         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
54     </bean>
55     
56     <!-- 事务管理器 -->
57     <bean id="transactionManager"
58         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
59         <property name="dataSource" ref="dataSource" />
60     </bean>
61     
62     <tx:annotation-driven transaction-manager="transactionManager" />
63 </beans>
mybatis-spring.xml
技术分享
 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" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xsi:schemaLocation="  
 7      http://www.springframework.org/schema/beans   
 8      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
 9      http://www.springframework.org/schema/context  
10      http://www.springframework.org/schema/context/spring-context-3.0.xsd  
11      http://www.springframework.org/schema/mvc  
12      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
13 
14      <!-- 启用spring mvc 注解 -->
15     <context:annotation-config />
16     
17     <!-- 设置使用注解的类所在的jar包 -->
18     <context:component-scan base-package="com.weiyi.controller" />    
19     
20     <!-- 完成请求和注解POJO的映射 -->
21     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
22 
23     <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
24     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
25         p:prefix="/WEB-INF/" p:suffix=".jsp" />
26     
27 </beans>
spring-mvc.xml
技术分享
 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" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6      http://www.springframework.org/schema/beans/spring-beans.xsd
 7      http://www.springframework.org/schema/context
 8      http://www.springframework.org/schema/context/spring-context.xsd
 9      http://www.springframework.org/schema/aop
10      http://www.springframework.org/schema/aop/spring-aop.xsd
11      http://www.springframework.org/schema/tx 
12      http://www.springframework.org/schema/tx/spring-tx.xsd">
13      
14     <!-- 自动注入 -->
15     <context:component-scan base-package="com.weiyi.service.impl" />
16     <!-- 加载properties文件  -->
17     <!-- <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
18         <property name="locations">
19             <list>
20                 <value>classpath:mysqldb.properties</value>
21             </list>
22         </property>
23     </bean> -->
24 </beans>
spring.xml

maven+SSM框架工程搭建

标签:--   html4   artifact   仓库   conf   语句   lap   1.0   ems   

原文地址:http://www.cnblogs.com/weiyi-1009/p/6761707.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!