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 ...
随机推荐
- cassandra cpp driver中bind list——用cass_statement_bind_collection函数
CassError insert_into_collections(CassSession* session, const char* key, const char* items[]) { Cass ...
- UVA-10827(前缀和降维)
题意: 给一个n*n的正方形,第一行和最后一行粘在一块,第一列和最后一列粘在一块,求这个环面上的最大的子矩形; 思路: 直接暴力是O(n^6)的复杂度,可以把前缀和求出来,这样就可以只用枚举四条边界就 ...
- macbook pro 一些操作经验记录
[一]启用root用户 (1)打开终端 (2)输入命令:sudo passwd root (3)给root设置密码 (4)然后再终端输入命令:su root 进行登陆 [二]解压eclipse压缩包, ...
- [SDOI 2008] 洞穴勘测
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2049 [算法] LCT动态维护森林连通性 时间复杂度 : O(NlogN ^ 2) ...
- 201响应为什么进了AJAX error回调函数
明明AJAX发送请求成功,但是后端返回的不是200,而是201,结果进了error的回调函数,想这种情况,只需要把“dataType:"json",改成dataType:" ...
- ecb-2.40与cedet-1.1的兼容(转载)
转自:http://blog.csdn.net/cnsword/article/details/7474119 今天凑热闹把fedora升级到了17,emacs升级到了24,但是悲剧了,显示cedet ...
- Android Service完全解析,关于服务你所需知道的一切(上) (转载)
转自:http://blog.csdn.net/guolin_blog/article/details/11952435 转载请注明出处:http://blog.csdn.net/guolin_blo ...
- poj1511【最短路spfa】
题意: 计算从源点1到各点的最短路之和+各点到源点1的最短路之和: 思路: 源点这个好做啊,可是各点到源点,转个弯就是反向建边然后求一下源点到各点的最短路,就是各点到源点的最短路,在两幅图里搞: 贴一 ...
- python __builtins__ bool类 (6)
6.'bool', 函数用于将给定参数转换为布尔类型,如果没有参数,返回 False. class bool(int) # 继承于int类型 | bool(x) -> bool # 创建boo ...
- bzoj 2131: 免费的馅饼【dp+树状数组】
简单粗暴的dp应该是把馅饼按时间排序然后设f[i]为i接到馅饼能获得的最大代价,转移是f[i]=max(f[j])+v[i],t[j]<=t[i],2t[i]-2t[j]>=abs(p[i ...