eclipse中搭建ssm框架

时间:2023-03-08 21:21:18

工具:jdk1.7+eclipse+tomcat+mysql。

这里用的版本是spring3,框架中用到的实体类和xml映射文件都可以用工具生成的。接下来会将源码贴出,方便初学者快速搭建。

一、新建一个web工程,添加目录包结构如下。

eclipse中搭建ssm框架

二、添加ssm所用的jar包。

eclipse中搭建ssm框架

三,修改web.xml代码如下。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ssm</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <!-- spring的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>Encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener> <!-- spring mvc核心:分发servlet -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- spring mvc的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<!-- 此处配置成*.do,对应struts的后缀习惯 -->
<url-pattern>*.do</url-pattern>
<!-- 此处配置成*.json,是为了能够截获"../*.json"格式的访问(例如android客户端) -->
<url-pattern>*.json</url-pattern>
</servlet-mapping> </web-app>

四、添加applicationContext.xml和springMVC.xml配置文件。并修改配置文件中对应的资源路径。这里先贴出各个文件的位置。

eclipse中搭建ssm框架

1.applicationContext.xml

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:annotation-config />
<context:component-scan base-package="com.gh.service" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/young?characterEncoding=UTF-8</value> </property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.gh.po" />
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:com/gh/mapper/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.gh.dao,com.gh.mapper"/>
</bean>
</beans>

2.springMVC.xml

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:annotation-config/> <context:component-scan base-package="com.gh.controller">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<!-- <property name="prefix" value="/WEB-INF/jsp/" /> -->
<property name="suffix" value=".jsp" />
</bean>
</beans>

五.编写测试代码。实现简单查询。

UserMapper.java

package com.gh.dao;

import java.util.List;

import com.gh.po.User;

public interface UserMapper {
int deleteByPrimaryKey(Integer userid); int insert(User record); int insertSelective(User record); User selectByPrimaryKey(Integer userid); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record);
List<User> selectAll();
}

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.gh.dao.UserMapper" >
<resultMap id="BaseResultMap" type="com.gh.po.User" >
<id column="userid" property="userid" jdbcType="INTEGER" />
<result column="userName" property="username" jdbcType="VARCHAR" />
<result column="pwd" property="pwd" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
userid, userName, pwd
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from user
where userid = #{userid,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from user
where userid = #{userid,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.gh.po.User" >
insert into user (userid, userName, pwd
)
values (#{userid,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{pwd,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="com.gh.po.User" >
insert into user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="userid != null" >
userid,
</if>
<if test="username != null" >
userName,
</if>
<if test="pwd != null" >
pwd,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="userid != null" >
#{userid,jdbcType=INTEGER},
</if>
<if test="username != null" >
#{username,jdbcType=VARCHAR},
</if>
<if test="pwd != null" >
#{pwd,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.gh.po.User" >
update user
<set >
<if test="username != null" >
userName = #{username,jdbcType=VARCHAR},
</if>
<if test="pwd != null" >
pwd = #{pwd,jdbcType=VARCHAR},
</if>
</set>
where userid = #{userid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.gh.po.User" >
update user
set userName = #{username,jdbcType=VARCHAR},
pwd = #{pwd,jdbcType=VARCHAR}
where userid = #{userid,jdbcType=INTEGER}
</update>
<select id="selectAll" resultType="com.gh.po.User">
select * from user
</select >
</mapper>

User.java

package com.gh.po;

public class User {
private Integer userid; private String username; private String pwd; public Integer getUserid() {
return userid;
} public void setUserid(Integer userid) {
this.userid = userid;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPwd() {
return pwd;
} public void setPwd(String pwd) {
this.pwd = pwd;
}
}

UserService.java

package com.gh.service;

import com.gh.dao.UserMapper;
public interface UserService extends UserMapper{ }

UserServiceimpl.java

package com.gh.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import com.gh.dao.UserMapper;
import com.gh.po.User;
import com.gh.service.UserService;
@Transactional
@Service
public class UserServiceimpl implements UserService{
@Autowired
private UserMapper us;
@Override
public int deleteByPrimaryKey(Integer userid) {
// TODO Auto-generated method stub
return 0;
} @Override
public int insert(User record) {
// TODO Auto-generated method stub
return 0;
} @Override
public int insertSelective(User record) {
// TODO Auto-generated method stub
return 0;
} @Override
public User selectByPrimaryKey(Integer userid) {
// TODO Auto-generated method stub
return null;
} @Override
public int updateByPrimaryKeySelective(User record) {
// TODO Auto-generated method stub
return 0;
} @Override
public int updateByPrimaryKey(User record) {
// TODO Auto-generated method stub
return 0;
} @Override
public List<User> selectAll() {
// TODO Auto-generated method stub
return us.selectAll();
} }

最好编写测试代码:

 package com.gh.controller;

 import java.util.List;

 import javax.annotation.Resource;

 import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.gh.po.User;
import com.gh.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class UserController {
@Resource
private UserService us;
@Test
public void selectAllUser(){
List <User> list=us.selectAll();
if(list.size()>0){
for (User user : list) {
System.out.println(user.getUsername());
}
} } }

打印出信息如下:说明框架成功运转。

[org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [applicationContext.xml]
  [org.springframework.context.support.GenericApplicationContext] - Refreshing org.springframework.context.support.GenericApplicationContext@1fe398a0: startup date [Tue Apr 24 16:00:53 CST 2018]; root of context hierarchy
  [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2f0cccfb: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,userServiceimpl,dataSource,sqlSession,org.mybatis.spring.mapper.MapperScannerConfigurer#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,userMapper]; root of factory hierarchy
  [org.springframework.jdbc.datasource.DriverManagerDataSource] - Loaded JDBC driver: com.mysql.jdbc.Driver
  华哥
陈哥
光哥
[org.springframework.context.support.GenericApplicationContext] - Closing org.springframework.context.support.GenericApplicationContext@1fe398a0: startup date [Tue Apr 24 16:00:53 CST 2018]; root of context hierarchy
  [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2f0cccfb: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,userServiceimpl,dataSource,sqlSession,org.mybatis.spring.mapper.MapperScannerConfigurer#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,userMapper]; root of factory hierarchy