aplicationContext.xml
<!-- spring 整合,配置mybatis -->
<bean id="sqlSessionFactory" class=" org.mybatis.spring.SqlSessionFactoryBean">
<!--dataSource属性指定要用到的连接池-->
<property name="dataSource" ref="dataSource"></property>
<!-- 所有配置的mapper文件,mapperLocations不能变 -->
<property name="mapperLocations" value="classpath*:com/hw/mapper/*Mapper.xml"></property>
</bean>
<!-- 配置bean 自动扫描所有mapper 自动给Mapper接口产生代理类对象 并且给代理对象注入SqlSessionFactory-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hw.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 配置扫描 -->
<context:component-scan base-package="com.hw" />
<!-- mvc:annotation-driven:使用mvc注解,解决406同时解决时间问题 -->
<mvc:annotation-driven>
<!-- 处理responseBody 里面日期类型 -->
<mvc:message-converters>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
</bean>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/per/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置spring mvc上传图片大小,multipartResolver名不能改 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="104857600"></property>
</bean>
</beans>
mybiatis-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>
<!-- 配置mysql数据库的连接环境 -->
<environments default="mybatis"><!-- 默认名可以任意,但要和其下的id一致 -->
<environment id="mybatis">
<!-- JDBC - 这个类型直接全部使用 JDBC 的提交和回滚功能。它依靠使用连接的数据源来管理事务的作用域。 MANAGED - 这个类型什么不做
, 它从不提交 、 回滚和关闭连接 。 而是让窗口来管理事务的全部生命周期 。(比如说 Spring 或者 JAVAEE 服务器) -->
<transactionManager type="JDBC"></transactionManager><!--
一般用JDBC -->
<dataSource type="POOLED"><!-- 一般用POOLED -->
<!-- 据源类型有三种: UNPOOLED , POOLED , JNDI 。 UNPOOLED - 这个数据源实现只是在每次请求的时候简单的打开和关闭一个连接。虽然这有点慢,但作为一些不需要性能和立即响应的简单应用来说
, 不失为一种好选择 。 POOLED - 这个数据源缓存 JDBC 连接对象用于避免每次都要连接和生成连接实例而需要的验证时间 。对于并发 WEB
应用,这种方式非常流行因为它有最快的响应时间。 JNDI - 这个数据源实现是为了准备和 Spring 或应用服务一起使用,可以在外部也可以在内部配置这个数据源,然后在
JNDI 上下文中引用它。这个数据源配置只需要两上属性: -->
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/qq?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<!-- 配置映射文件 -->
<mappers>
<mapper resource="com/hw/mapper/UserDaoMapper.xml"/>
<mapper resource="com/hw/mapper/DeptDaoMapper.xml"/>
<mapper resource="com/hw/mapper/PersonDaoMapper.xml"/>
</mappers>
</configuration>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<!-- 配置spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 如果applicationContext.xml不在web-info下则要配置 -->
<context-param>
<param-name>contextConfigLocation</param-name><!--contextConfigLocation名不能改 -->
<param-value>classpath:config/applicationContext.xml</param-value>
</context-param>
<!-- 配置spring mvc -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 如果qq-servlet.xml放在web-info文件夹下则不用配置,contextConfigLocation名不能改 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/qq-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern><!-- 是spring mvc开发习惯要.do结尾,struts要用.action -->
</servlet-mapping>
<!-- spring mvc处理乱码 -->
<filter>
<filter-name>bm</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- 设置编码参数 -->
<init-param>
<param-name>encoding</param-name><!--encoding不能变 -->
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>bm</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
mapper.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">
<!--namespace命名空间如果和spring整合时要用接口全类名 -->
<mapper namespace="com.hw.dao.UserDao">
<resultMap type="com.hw.entity.User" id="userinfo">
<result column="id" property="id"/>
<result column="uname" property="uname"/><!--column和property不能省 -->
<result column="pwd" property="pwd"/>
</resultMap>
<insert id="saveUser" parameterType="com.hw.entity.User"><!-- parameterType参数类型 -->
insert into tab_user values(null,#{uname},#{pwd},#{js});
</insert>
<delete id="delUser" parameterType="int"><!-- 删除单个 -->
delete from tab_user where id=#{id}
</delete>
<delete id="delAllUser" parameterType="string"><!--删除多个 -->
delete from tab_user where id in
<foreach collection="array" item="id" open="(" close=")"
separator=",">
#{id}
</foreach>
</delete>
<update id="updateUser" parameterType="com.hw.entity.User">
update tab_user
<set>
<choose>
<when test="uname!=null">
uname=#{uname}
</when>
<when test="pwd!=null">
pwd=#{pwd}
</when>
<when test="js!=null">
js=#{js}
</when>
</choose>
</set>
where id=#{id}
</update>
<select id="list" resultMap="userinfo">
<!-- resultType="com.hw.entity.User"返回结果类型,也可以用 resultMap="userinfo" -->
select * from tab_user
</select>
<select id="getUser" resultType="com.hw.entity.User"
parameterType="int">
select * from tab_user where id=#{id}
</select>
<select id="getCountUser" resultType="int">
select count(id) from
tab_user
</select>
<select id="ListPage" resultMap="userinfo"
parameterType="map">
<!-- 如果开发模糊查询时用#则调用 时需要:map.put("uname", "%张%");map.put("js", "%管理员%");
select * from tab_user where uname like #{uname} and js like #{js} limit
#{start},#{end} -->
select * from tab_user where 1=1
<if test="uname!=null">
and uname like '%${uname}%'
</if>
<if test="js!=null">
and js like '%${js}%'
</if>
order by id desc limit #{start},#{end}
</select>
<select id="getCountListUser" parameterType="map" resultType="int">
select count(id) from tab_user where 1=1
<if test="uname!=null">
and uname like '%${uname}%'
</if>
<if test="js!=null">
and js like '%${js}%'
</if>
</select>
</mapper>
网友评论