ssm框架搭建(上)
前言
之前也说过,工作做的开发都是基于公司现有的框架,心里很没底。所以一直想自己能够搭建出ssm框架。经过多次尝试,终于成功了。这边文章将从两个方面进行,一是框架搭建,二是简单的增删查改。
正文
1.环境搭建
这里没有采用采用现在流行的maven方式,而是将需要的jar放在web_inf\lib下面了。

直接贴了一张图,有点任性了...整个工程的结构如下图所示

在conf子包中,是配置文件。mapper下对应mybatis的映射文件,里面包含了相应的sql语句。(mvcLearn\conf\mapper\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:接口包 + 接口名 -->
<mapper namespace="org.tonny.map.IUserMapper">
<select id="getUserById" parameterType="int" resultType="org.tonny.model.User">
SELECT *
FROM users WHERE id=#{id}
</select> <select id="getAll" resultType="org.tonny.model.User">
SELECT *
FROM users
</select> <insert id="add" parameterType="org.tonny.model.User">
INSERT INTO users(name,age)
VALUES(#{name}, #{age})
</insert> <update id="update" parameterType="org.tonny.model.User">
UPDATE users
SET name = #{name},age = #{age}
WHERE id=#{id}
</update> <delete id="delete" parameterType="int">
DELETE FROM users
WHERE id=#{id}
</delete>
</mapper>
db.properties是数据库相关的信息,如ip,用户名,密码等。
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis
username=root
password=1qaz2wsx
log4j.properties是log日志的配置文件,记录了输出级别,输出形式等。
# Root logger option
log4j.rootLogger=INFO, stdout # Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
mybatis.xml是mybatis的配置文件,里面描述了mybatis的配置信息,如别名,mybatis的日志属性等。
<?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="logImpl" value="LOG4J"/>
</settings> <!-- 配置映射类的别名 -->
<typeAliases>
<typeAlias alias="User" type="org.tonny.model.User" />
</typeAliases>
<!-- 配置Mapper文件的路径 -->
<mappers>
<!-- <mapper resource="mapper/UserMapper.xml" /> -->
</mappers>
</configuration>
spring-mvc.xml是springmvc的配置文件,里面配置了如分发器,jsp前缀后缀等。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <mvc:annotation-driven /> <!-- 把标记了@Controller注解的类转换为bean,这个里面只要关注控制器,其他的bean在spring中去扫描 -->
<context:component-scan base-package="org.tonny.controller" /> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/view/" p:suffix=".jsp" /> </beans>
spring.xml则描述了bean的配置,一些配置文件之间的关系则会在这里体现。
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <!-- 自动扫描注解的bean -->
<context:component-scan base-package="org.tonny" /> <!-- 引入配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:db.properties</value>
</list>
</property>
</bean> <!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean> <!-- 引用Mapper文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 设置mybatis文件 -->
<property name="configLocation" value="classpath:mybatis.xml"></property> <!-- 扫描mybatis表对应的文件 -->
<property name="mapperLocations" value="classpath*:mapper/*Mapper.xml" />
</bean> <!-- 配置接口与mapper.xml的对应关系 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--
如果使用
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
会发生错误,因为db.propertites加载在sqlSessionFactory之后,所以使用sqlSessionFactoryBeanName代替
-->
<!-- 配置mapper对应的接口,指定所在的包 -->
<property name="basePackage" value="org.tonny.map" />
</bean>
</beans>
最后是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"> <!-- Spring 容器加载 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param> <!-- log4j 这一段不配置,log4j配置文件也是可以使用的,这边应该单独给Spring的使用-->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>600000</param-value>
</context-param> <!-- SpringMVC的前端控制器 -->
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载配置文件路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!-- 何时启动 大于0的值表示容器启动时初始化此servlet,正值越小优先级越高 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Spring MVC配置文件结束 --> <!-- SpringMVC拦截设置 -->
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<!-- 由SpringMVC拦截所有请求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- SpringMVC拦截设置结束 --> <!--解决中文乱码问题 -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
ssm框架搭建(上)的更多相关文章
- SSM框架搭建web服务器实现登录功能(Spring+SpringMVC+Mybatis)
初学java EE,虽然知道使用框架会使开发更加便捷高效,但是对于初学者来说,感到使用框架比较迷惑,尤其是各种jar包的引用.各种框架的配置.注解的使用等等. 最好的学习方法就是实践,于是下载了一个现 ...
- SSM 框架搭建
SSM框架搭建(Spring.SpringMVC.Mybatis) 一:基本概念 Spring : Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框 ...
- SSM框架搭建教程(从零开始,图文结合)
1.准备 IntelliJ IDEA Tomcat JDK Maven mysql spring.springmvc.mybatis 了解 现在假设如上条件你都具备,那么通过我这篇博客 你一定可以整合 ...
- 实习小结(二)--- SSM框架搭建
SSM项目框架搭建 前几天做了一个学生信息管理的项目,使用纯控制台输入,查询数据库,将信息在控制台中打印,功能完善得差不多之后,老师让将这个项目移植到Web中,使用Spring+SpringMVC+M ...
- SpringMVC笔记——SSM框架搭建简单实例
落叶枫桥 博客园 首页 新随笔 联系 订阅 管理 SpringMVC笔记——SSM框架搭建简单实例 简介 Spring+SpringMVC+MyBatis框架(SSM)是比较热门的中小型企业级项目开发 ...
- SSM框架搭建详细解析
总结了一下搭建SSM框架流程,在以后用到的时候方便回头使用. 使用工具:MyEclipse 2015:Tomcat 8版本:jdk1.8版本. 首先: 1:创建一个WebProject项目,jdk1. ...
- idea ssm框架搭建
1.分享一篇完整的ssm框架搭建连接 大牛博客:https://www.cnblogs.com/toutou/p/ssm_spring.html#_nav_0 2.我的搭建的完整项目连接,可以进入我的 ...
- ssm框架搭建整合测试
下载各种jar包 mybatis下载 https://github.com/mybatis/mybatis-3/releases mysql驱动下载 http://mvnrepository.com/ ...
- SSM框架搭建(转发)
SSM框架,顾名思义,就是Spring+SpringMVC+mybatis. 通过Spring来将各层进行整合, 通过spring来管理持久层(mybatis), 通过spring来管理handler ...
随机推荐
- LA-4726 (斜率优化+单调队列)
题意: 给定一个01序列,选一个长度至少为L 的连续子序列使其平均值最大;输出这个子序列的起点和终点;如果有多个答案,输出长度最小的,还有多个就输出第一个编号最小的; 思路: 用sum[i]表示[1, ...
- 【POJ 3580】 SuperMemo
[题目链接] 点击打开链接 [算法] 本题也是Splay区间操作的模板题,不过要比BZOJ 3223要稍微复杂一些,做完此题后,我终于对Splay有了更深入的理解,有“拨开云雾见青天”的感觉 本题还是 ...
- 【转】Chrome调试鼠标悬停后出现的元素
原文地址:https://blog.csdn.net/sparrowflying/article/details/80996550 调试小技巧:调试样式的时候,有一类元素是鼠标悬停在特定位置才会出现的 ...
- 一个简单的backbone实例(基于139邮箱)
先看一下效果图: 代码如下: <!doctype html> <html lang="en"> <head> <meta http-equ ...
- web_html-day1
概述 HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这样就可以让浏览器 ...
- Task用法
转: https://www.cnblogs.com/wyy1234/p/9172467.html
- Table View Programming Guide for iOS---(五)---Creating and Configuring a Table View
Creating and Configuring a Table View Your app must present a table view to users before it can mana ...
- OkHttp解析
今天花了一天时间研究了下OkHttp3的内部原理,记录在此处以便后期查阅 我们先来看下基本的使用方式: public void sendHttpRequest(String url,Callback ...
- C++笔试题库之编程、问答题 300~305道
301. 以下代码有什么问题? cout << (true?1:”1″) << endl; 答:三元表达式“?:”问号后面的两个操作数必须为同一类型. 302.以下代码能够编译 ...
- thinkphp整合swoole
cli模式下执行thinkphp1.cd 项目根目录2.php index.php admin/index/index --执行 模块/控制器/方法名 异步消息队列1.服务器端核心代码 /** * 脚 ...