spring-ajax.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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
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-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 组件扫描 -->
<context:component-scan base-package="cn.tedu.ajax"></context:component-scan>
<!-- 注解驱动 :如果没有添加该注解驱动,会出现406错误不可接受-->
<mvc:annotation-driven />
</beans>
spring-dao.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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
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-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<util:properties id="config"
location="classpath:db.properties"></util:properties>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="#{config.url}"></property>
<property name="driverClassName" value="#{config.driver}"></property>
<property name="username" value="#{config.username}"></property>
<property name="password" value="#{config.password}"></property>
<property name="initialSize" value="#{config.initialSize}"></property>
<property name="maxActive" value="#{config.maxActive}"></property>
</bean>
<!-- 配置MapperScannerConfigurer -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--basePackage:根包 -->
<property name="basePackage" value="cn.tedu.mybatis"></property>
</bean>
<!-- 配置SqlSessionFactoryBean -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" >
<!-- 配置xml文件在哪里 -->
<property name="mapperLocations" value="classpath:mappers/*.xml"></property>
<!-- 配置使用哪个数据源连接数据库 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
spring-mvc.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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
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-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 组件扫描 -->
<context:component-scan base-package="cn.tedu.spring"></context:component-scan>
<!-- 模板解析器:ServletContextTemplateResolver html文件应该放在WEB-INF下 -->
<!-- 模板解析器:ClassLoaderTemplateResolver html文件应该放在resources下-->
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/"></property>
<property name="suffix" value=".html"></property>
<property name="characterEncoding" value="utf-8"></property>
<property name="templateMode" value="HTML"></property>
<property name="cacheable" value="false"></property>
</bean>
<!-- Spring模板引擎:SpringTemplateEngine -->
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver"></property>
</bean>
<!-- 视图解析器:ThymeleafViewResolver -->
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine"></property>
<property name="characterEncoding" value="utf-8"></property>
</bean>
<!-- 配置拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- 拦截路径 -->
<mvc:mapping path="/user/success.do"/>
<bean class="cn.tedu.spring.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/数据库名字?useUnicode=true&characterEncoding=utf-8
username=root
password=root
initialSize=2
maxActive=10
SomeMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<!-- 对应哪个接口 -->
<mapper namespace="cn.tedu.mybatis.UserMapper">
<!-- id:抽象方法的名称(抽象方法中不能使用方法重载,不然配置文件不知道是哪一个) -->
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
<!-- useGeneratedKeys="true" keyProperty="id"获取自动增长的值,并封装到指定的"id"属性中去 -->
insert into t_user (username,password,age,phone,email,is_delete) values
(#{username},#{password},#{age},#{phone},#{email},#{isDelete})
</insert>
<delete id="deleteById">
delete from t_user where id=#{id};
</delete>
<update id="updateAllPassword">
update t_user set password=#{password}
</update>
<!-- Integer updateEmailById(@Param("id") Integer id,@Param("email") String
email); -->
<update id="updateEmailById">
UPDATE t_user SET email=#{email} WHERE id=#{id}
</update>
<select id="count" resultType="java.lang.Integer">
select count(*) from t_user
</select>
<select id="selectId" resultType="cn.tedu.mybatis.User">
select * from t_user where id=#{id}
</select>
<select id="selectMaxAge" resultType="cn.tedu.mybatis.User">
select * from t_user order by age desc limit 0,1
</select>
<!-- id:自定义名称 -->
<resultMap type="cn.tedu.mybatis.User" id="UserMap">
<!-- id节点:配置主键对应关系 column:查询结果的列名 property:属性名-->
<id column="id" property="id"/>
<!-- result节点:配置其他对应关系 -->
<result column="username" property="username"/>
<result column="password" property="password"/>
<result column="age" property="age"/>
<result column="phone" property="phone"/>
<result column="email" property="email"/>
<result column="is_delete" property="isDelete"/>
</resultMap>
<!-- List<User> selectAll(); -->
<select id="selectAll" resultMap="UserMap">
select * from t_user
</select>
<!-- Integer selectByIds(List<Integer> ids); -->
<select id="selectByIds" resultType="cn.tedu.mybatis.User">
select * from t_user where id in(
<foreach collection="list" separator="," item="id">
#{id}
</foreach>
)
</select>
<!-- List<User> select( @Param("where")String where,@Param("orderBy")String
orderBy, @Param("offset")Integer offset,@Param("count")Integer count ); -->
<select id="select" resultType="cn.tedu.mybatis.User">
select * from t_user
<if test="where!=null">where ${where}</if>
<if test="orderBy!=null">order by ${orderBy}</if>
<if test="offset!=null and count!=null">limit #{offset},#{count}</if>
</select>
<!-- List<VOUser> selectJoinOn(); -->
<resultMap type="cn.tedu.mybatis.VOUser" id="UserVo">
<!-- id节点:配置主键对应关系 column:查询结果的列名 property:属性名-->
<id column="id" property="id"/>
<!-- result节点:配置其他对应关系 -->
<result column="name" property="department_name"/>
</resultMap>
<select id="selectJoinOn" resultMap="UserVo">
SELECT tu.`id`,tu.`username`,tu.`password`,tu.`age`,tu.`email`,tu.`phone`,tu.`is_delete`,tu.`department_id`,td.`name` FROM t_user tu LEFT JOIN t_department td ON tu.`department_id`=td.`id`
</select>
</mapper>