maven+SSM框架工程搭建

来源:互联网 发布:知名品牌网络授权 编辑:程序博客网 时间:2024/06/05 23:03

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

2.使用最新版eclipse 集成maven

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

3.新建工程

项目原型选择webapp项目

 

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

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

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

 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 @Controller11 @RequestMapping(value="user")12 public class UserController {13     @Autowired14     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 }
复制代码
复制代码
package com.weiyi.dao;import java.util.List;import com.weiyi.entity.User;public interface UserDao {    List<User> queryAll();}
复制代码
复制代码
 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 stub10     }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     @Override36     public String toString() {37         return "User [id=" + id + ", username=" + username + ", password=" + password + "]";38     }39     40 }
复制代码
复制代码
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 }
复制代码
复制代码
 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 @Service12 public class UserServiceImpl implements UserService {13     @Autowired14     UserDao dao;15     16     public List<User> userList() {17         return dao.queryAll();18     }19 20 }
复制代码
复制代码
<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>
复制代码
复制代码
 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>
复制代码

下面是配置文件

复制代码
 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>
复制代码
1 log4j.rootLogger=DEBUG, stdout2 log4j.logger.org.mybatis=DEBUG3 log4j.appender.stdout=org.apache.log4j.ConsoleAppender4 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout5 log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n
复制代码
 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/aop10      http://www.springframework.org/schema/aop/spring-aop.xsd11      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>
复制代码
复制代码
 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>
复制代码
复制代码
 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/aop10      http://www.springframework.org/schema/aop/spring-aop.xsd11      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>
复制代码
0 0
原创粉丝点击