SSM整合
来源:互联网 发布:网络机柜的尺寸 编辑:程序博客网 时间:2024/05/17 04:44
一,导入jar包
jar包和入手项目下载地址:
二,把applicationContext.xml放到WEB-INF下 配置数据库相关参数
<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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:property-placeholder location="classpath:app.properties" /> <!--创建jdbc数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <property name="maxActive" value="${maxActive}" /> <property name="maxIdle" value="${maxIdle}" /> <property name="maxWait" value="${maxWait}" /> </bean> <!-- 创建SqlSessionFactory,同时指定数据源 --> <!-- SqlSessionFactoryBean相当于SqlSessionFactoryBuilder --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 指定mybtis总配置文件,订制的environment在spring容器中不在生效 --> <property name="configLocation" value="classpath:conf.xml" /> </bean> <!--SqlSessionTemplate 是对SqlSession接口的一个实现 以前使用纯mybatis时 使用的是DefaultSqlSession这个类 --> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean> <bean id="userDaoImpl" class="com.dao.impl.UserDaoImpl"> <property name="session" ref="sqlSession" /> </bean> <bean id="userService" class="com.service.impl.UserServiceImpl"> <property name="dao" ref="userDaoImpl" /> </bean></beans>
三,在web.xml中配置SpringMVC参数和一个监听器 ContextLoaderListener
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>SSM</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 第三步: 配置前端控制器 DispatcherServlet --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 如果不配置contextConfigLocation 默认读取的是 WEB-INF/servlet名-servlet.xml(也就是springmvc-servlet.xml) 这个文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- *.action 客户端发出*.action的请求时 被控制器处理 --> <url-pattern>*.action</url-pattern> </servlet-mapping> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener></web-app>
四,配置userMapper
<?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.po.userMapper"> <insert id="addUser" parameterType="User"> insert into user values(null,#{name},#{age}); </insert></mapper>
五,配置 conf.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="mapUnderscoreToCamelCase" value="true" /> <!--打开延迟加载的开关 --> <setting name="lazyLoadingEnabled" value="true" /> <!--将积极加载改为消极加载及按需加载 --> <setting name="aggressiveLazyLoading" value="false" /> </settings> <typeAliases> <!-- <typeAlias type="com.po.User" alias="_User"/> --> <package name="com.po" /> </typeAliases> <mappers> <mapper resource="com/po/userMapper.xml" /> </mappers></configuration>
写dao,service,controller
最后配springmvc.xml
<bean class="com.controller.UserController"> <property name="userService" ref="userService"></property></bean>
0 0
- SSM整合
- SSM整合
- SSM整合
- ssm整合
- SSM整合
- ssm整合
- ssm整合
- SSM整合
- SSM整合
- SSM整合
- SSM整合
- ssm整合
- ssm整合
- ssm整合
- ssm整合
- ssm整合
- ssm整合
- ssm整合
- 用python简单处理图片(2):图像通道\几何变换\裁剪
- SpringMvc Bean转Json格式
- opengl学习第二天
- 自定义控件其实很简单1/4
- 分治法实验-寻找第k小元素
- SSM整合
- Spark 基础函数
- android基础笔记----Activity的启动与之间的信息传递
- Spring MVC 和 Struts2 的对比
- qduoj 码农必修(x或y等于x加y)
- 用python简单处理图片(3):添加水印
- python 解析 mach-o
- ProgressDialog的使用-------------范例(学习笔记)
- 基于CentOS的Cifs(samba)文件系统搭建