从零开始搭建SSM框架

 项目源码:https://pan.baidu.com/s/1Bu8Icwy0eXcN38GesS10Yg

下面我们利用SSM做一个web项目,怎么创建web项目不再多说。

 1、建库

注意:由于之前写这篇文章时,没有附带数据库, 现在补一张建库图,因此造成数据与下面显示不一致,这这里说声抱歉。

都知道“巧妇难为无米之炊”,因此下面我们开始先导入jar包

1、导入ssm框架所需的jar包

 SSM框架jar包:https://pan.baidu.com/s/1Qtb9KWHHxcFpUBrjzdlY5A

2、 搭建项目的目录结构

注意: resources这是一个Source Folder

 

 3、添加各种配置文件

 

3_1、database.properties

driver=com.mysql.jdbc.Driver
#在和mysql传递数据的过程中,使用unicode编码格式,并且字符集设置为utf-8
url=jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8
user=root
password=root

 3_2、log4j.properties

log4j.rootLogger=debug,CONSOLE,file
#log4j.rootLogger=ERROR,ROLLING_FILE

log4j.logger.cn.smbms=debug
log4j.logger.org.apache.ibatis=debug
log4j.logger.org.mybatis.spring=debug
log4j.logger.java.sql.Connection=debug
log4j.logger.java.sql.Statement=debug
log4j.logger.java.sql.PreparedStatement=debug
log4j.logger.java.sql.ResultSet=debug

######################################################################################
# Console Appender  \u65e5\u5fd7\u5728\u63a7\u5236\u8f93\u51fa\u914d\u7f6e
######################################################################################
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=debug
log4j.appender.CONSOLE.DatePattern=yyyy-MM-dd
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= - (%r ms) - %d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n



######################################################################################
# Rolling File  \u6587\u4ef6\u5927\u5c0f\u5230\u8fbe\u6307\u5b9a\u5c3a\u5bf8\u7684\u65f6\u5019\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6
######################################################################################
#log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender
#log4j.appender.ROLLING_FILE.Threshold=INFO
#log4j.appender.ROLLING_FILE.File=${baojia.root}/logs/log.log
#log4j.appender.ROLLING_FILE.Append=true
#log4j.appender.ROLLING_FILE.MaxFileSize=5000KB
#log4j.appender.ROLLING_FILE.MaxBackupIndex=100
#log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout
#log4j.appender.ROLLING_FILE.layout.ConversionPattern=%d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n

######################################################################################
# DailyRolling File  \u6bcf\u5929\u4ea7\u751f\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u683c\u5f0f:log2009-09-11
######################################################################################
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern=yyyy-MM-dd
log4j.appender.file.File=${SMBMS_C09_04.root}/logs/log.log
log4j.appender.file.Append=true
log4j.appender.file.Threshold=debug
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern= - (%r ms) - %d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n

#DWR \u65e5\u5fd7
#log4j.logger.org.directwebremoting = ERROR

#\u663e\u793aHibernate\u5360\u4f4d\u7b26\u7ed1\u5b9a\u503c\u53ca\u8fd4\u56de\u503c
#log4j.logger.org.hibernate.type=DEBUG,CONSOLE 

#log4j.logger.org.springframework.transaction=DEBUG
#log4j.logger.org.hibernate=DEBUG
#log4j.logger.org.acegisecurity=DEBUG
#log4j.logger.org.apache.myfaces=TRACE
#log4j.logger.org.quartz=DEBUG

#log4j.logger.com.opensymphony=INFO  
#log4j.logger.org.apache.struts2=DEBUG  
log4j.logger.com.opensymphony.xwork2=debug

 3_3、mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

	<settings>
		<!-- 设置全局懒加载 -->
		<setting name="lazyLoadingEnabled" value="false" />
	</settings>

	<!-- 类型别名 -->
	<typeAliases>
		<package name="com.bdqn.pojo" />
	</typeAliases>
</configuration>

 3_4、applicationContext-mybatis.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        
    <context:component-scan base-package="com.bdqn.dao..*"/> 
    <context:component-scan base-package="com.bdqn.service..*"/>
    
    <!-- 读取数据库配置文件 -->
    <context:property-placeholder location="classpath:database.properties"/>
    <!-- JNDI获取数据源(使用dbcp连接池) -->  
    <bean id="dataSource" 
    		  class="org.apache.commons.dbcp.BasicDataSource" 
    		  destroy-method="close" scope="singleton">
    		<property name="driverClassName" value="${driver}" />  
			<property name="url" value="${url}" />  
			<property name="username" value="${user}" />  
			<property name="password" value="${password}" />
    </bean>
    
   	<!-- 配置mybitas SqlSessionFactoryBean-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    	<property name="dataSource" ref="dataSource"/>
    	<property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
	
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
         <property name="basePackage" value="com.bdqn.dao" />  
    </bean>
       
</beans>

3_5、springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    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.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
     
	<!-- 开启注解 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	<!-- 扫描注解的控制器 -->
	<context:component-scan base-package="com.bdqn.controller"/>
	
	<!-- 完成视图的对应 -->
	<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
		<!-- 配置原始图解析器 -->
		<property name="viewResolvers">
			<list>
				<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
					<property name="prefix" value="/WEB-INF/jsp/"/>
					<property name="suffix" value=".jsp"/>
				</bean>
			</list>
		</property>
	</bean>
	
</beans>

 4、修改web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

	<welcome-file-list>
		<welcome-file>/WEB-INF/jsp/login.jsp</welcome-file>
	</welcome-file-list>

	
  <display-name>springMVC</display-name>	
  <context-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:applicationContext-*.xml</param-value>
  </context-param>
  <listener>
  		<listener-class>
  			org.springframework.web.context.ContextLoaderListener
  		</listener-class>
  </listener>

	<!-- 配置spring mvc的核心控制器DispatcherServlet -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 初始化参数 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>

 5、创建pojo

 

package com.bdqn.pojo;

public class User {

	private Integer id;
	private String userCode;
	private String userName;
	private String userPassword;

	//......省略部分代码


}

6、dao层 

注意: 我在dao层创建了user包,这个包存放dao层用户的信息。

 

 6_1、UserMapper.java

package com.bdqn.dao.user;

import java.util.List;

import com.bdqn.pojo.User;

public interface UserMapper {
	
	/**
	 * 查询所有用户
	 * @return
	 */
	List<User> getUserList();
	

}

 6_2、UserMapper.xml

<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 
<mapper namespace="com.bdqn.dao.user.UserMapper">
	 
	 <select id="getUserList" resultType="user" parameterType="user">
	 	select * from smbms_user
	 </select>
	 
</mapper>

7、service层

 注意: 我在service层创建了user包,这个包存放service层用户的信息。

 

 7_1、UserService.java

package com.bdqn.service.user;

import java.util.List;

import com.bdqn.pojo.User;

public interface UserService {
	
	List<User> getUserList();

}

 7_2、UserServiceImpl.java

package com.bdqn.service.user;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.bdqn.dao.user.UserMapper;
import com.bdqn.pojo.User;

@Service("userService")
public class UserServiceImpl implements UserService {
	
	@Resource
	private UserMapper UserMapper;

	@Override
	public List<User> getUserList() {
		return UserMapper.getUserList();
	}

}

8、编写控制器

package com.bdqn.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bdqn.pojo.User;
import com.bdqn.service.user.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Resource
	private UserService userService;
	
	@RequestMapping("userList.html")
	public String getUserLists(Model model){
		List<User> userList=userService.getUserList();
		model.addAttribute("userList", userList);
		return "index";
	}

}

 9、新增index.jsp页面

 

 9_1、index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>查询所有用户列表</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
  		<table border="1" align="center" style="display : fixed;width : 30%;">
  				<tr>
  					<th>id</th>
  					<th>用户编码</th>
  					<th>用户名称</th>
  					<th>用户密码</th>
  				</tr>
  				<c:forEach items="${userList}" var="user" varStatus="status">
		  		<tr>
		  			<td>${user.id }</td>
		  			<td>${user.userCode }</td>
		  			<td>${user.userName }</td>
		  			<td>${user.userPassword }</td>
		  		</tr>
		  		</c:forEach>
  		</table>
  		<div style="width: 650px;margin: 0px auto;">
  			<h1 style="color: green;">勤学似春起之苗,不见其增,而日有所长。</h1>
  		</div>
  </body>
</html>

10、访问页面

访问地址:http://localhost:8080/ssm/user/userList.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值