1.项目需要的jar包
2.在web.xml中配置spring、springMVC
spring配置
<!-- 载入Spring ApplicationContext -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 设置Spring容器加载配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</context-param>
配置springMVC
<!-- 配置Spring核心控制器 -->
<servlet>
<servlet-name>springmvc</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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- url配置为/,不带文件后缀,会造成其它静态文件(js,css等)不能访问。如配为*.do,则不影响静态文件的访问 -->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
3.在src下添加全局配置文件
src下添加配置文件application.xml,与上述spring配置命名一致
<?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:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 使用import简化spring的配置文件 -->
<import resource="config/*" />
</beans>
application.xml中导入的配置文件包括jdbc.xml和mybatis.xml
src下添加配置文件spring-mvc.xml,与上述spring核心控制器配置的命名一致
4.jdbc.xml配置动态数据源
此处不作说明
5.mybatis.xml配置sqlSessionFactory,sqlSessionTemplate,MapperScannerConfigurer
在sqlSeesionFactory中需注入动态数据源dynamicDataSource,设置configLocation,也就是mybatis的xml配置文件路径,完成一些关于mybaits的配置。设置mapperLocations,也就是MyBatis数据操作的xml配置文件路径。
<!-- define the SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dynamicDataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="mapperLocations" value="classpath:com/silvery/project/*/dao/mapper/*.xml" />
</bean>
在sqlSessionTemplate中需要注入sqlSessionFactory
<!-- use the sqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
MapperScannerConfigurer用于扫描需要实现的mapper接口,即下面的BeanDao接口
<!-- scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.silvery.project.*.dao" />
<property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate" />
</bean>
6.定义一个IDao接口,所有BeanDao接口继承此接口。
此为一个IDao示例
package com.silvery.core.joggle;
import java.io.Serializable;
import java.util.List;
/**
* DAO层基本接口方法
*
* @author shadow
* @email 124010356@qq.com
* @create 2012.04.28
*
* @param <T>
* 实体
* @param <Q>
* 实体
*/
public interface IDao<T extends Serializable, Q extends Serializable> {
/**
* 保存
*
* @param model
* @return 记录数
* @throws Exception
*/
public abstract int insert(T model) throws Exception;
/**
* 批量保存
*
* @param models
* @return 记录数
* @throws Exception
*/
public abstract int insert4batch(List<T> models) throws Exception;
/**
* 更新
*
* @param model
* @return 记录数
* @throws Exception
*/
public abstract int update(T model) throws Exception;
/**
* 批量更新
*
* @param models
* @return 记录数
* @throws Exception
*/
public abstract int update4batch(List<T> models) throws Exception;
/**
* 删除
*
* @param pk
* @return 记录数
* @throws Exception
*/
public abstract int delete(T model) throws Exception;
/**
* 批量删除
*
* @param models
* @return 记录数
* @throws Exception
*/
public abstract int delete4batch(List<T> models) throws Exception;
/**
* 条件查询
*
* @param vo
* @return 记录集合
* @throws Exception
*/
public abstract List<T> query(Q vo) throws Exception;
/**
* 通过ID查询记录
*
* @param pk
* @return 记录数据
* @throws Exception
*/
public abstract T get(T model) throws Exception;
/**
* 分页查询记录数
*
* @param vo
* @return 分页总记录数
* @throws Exception
*/
public abstract int page4total(Q vo) throws Exception;
/**
* 分查询数据
*
* @param vo
* @return 分页数据
* @throws Exception
*/
public abstract List<T> page4list(Q vo) throws Exception;
}
7.BeanDao是MyBatis数据操作接口,此接口不需要实现
如此示例
package com.silvery.project.cms.dao;
import java.util.List;
import com.silvery.core.joggle.IDao;
import com.silvery.project.cms.model.AdminMeta;
import com.silvery.project.cms.model.AdminRole;
import com.silvery.project.cms.vo.AdminMetaVo;
public interface AdminMetaDao extends IDao<AdminMeta, AdminMetaVo> {
/**
* 查询资源授权角色
*
* @param vo
* @return List<AdminRole>
*/
public abstract List<AdminRole> update4authorize_init(AdminMetaVo vo);
/**
* 授权资源的角色
*
* @param vo
*/
public abstract void update4authorize(AdminMetaVo vo);
/**
* 获取资源的所有角色
*
* @param vo
* @return List<AdminRole>
*/
public abstract List<AdminRole> query4roles(AdminMetaVo vo);
}
8.在BeanDao接口的同一个包下创建mapper,在mapper中添加bean-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和定义的Dao接口对应,并实现其中的方法 -->
<mapper namespace="com.silvery.project.cms.dao.AdminMetaDao">
<!-- id和Dao接口中的方法名对应,parameterType使用完全路径,如果在mybatis.xml中配置了typeAliasesPackage则只需要些实体名 -->
<insert id="insert" parameterType="com.silvery.project.cms.model.AdminMeta">
insert into shop_meta
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="name != null">name,</if>
<if test="url != null">url,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">#{id},</if>
<if test="name != null">#{name},</if>
<if test="url != null">#{url},</if>
</trim>
</insert>
<update id="update" parameterType="com.silvery.project.cms.model.AdminMeta">
update shop_meta
<set>
<if test="name != null">name = #{name},</if>
<if test="url != null">url = #{url},</if>
</set>
where id = #{id}
</update>
<delete id="delete" parameterType="com.silvery.project.cms.model.AdminMeta">
delete from shop_meta
where id
= #{id}
</delete>
<select id="get" parameterType="com.silvery.project.cms.vo.AdminMetaVo"
resultType="com.silvery.project.cms.model.AdminMeta">
select * from shop_meta where id = #{id}
</select>
<select id="query" parameterType="com.silvery.project.cms.vo.AdminMetaVo"
resultType="com.silvery.project.cms.model.AdminMeta">
select * from shop_meta where 1 = 1
<if test="name != null and name != ''">and name like CONCAT(CONCAT('%', #{name}),'%')</if>
<if test="url != null and url != ''">and url like CONCAT(CONCAT('%', #{url}),'%')</if>
</select>
<select id="page4total" parameterType="com.silvery.project.cms.vo.AdminMetaVo"
resultType="int">
select count(1) from shop_meta where 1 = 1
<if test="name != null and name != ''">and name like CONCAT(CONCAT('%', #{name}),'%')</if>
<if test="url != null and url != ''">and url like CONCAT(CONCAT('%', #{url}),'%')</if>
</select>
<select id="page4list" parameterType="com.silvery.project.cms.vo.AdminMetaVo"
resultType="com.silvery.project.cms.model.AdminMeta">
select * from shop_meta where 1 = 1
<if test="name != null and name != ''">and name like CONCAT(CONCAT('%', #{name}),'%')</if>
<if test="url != null and url != ''">and url like CONCAT(CONCAT('%', #{url}),'%')</if>
and id >= (select id from shop_meta order by id limit #{index}, 1)
limit #{limit}
</select>
<insert id="insert4batch" parameterType="List">
insert into shop_meta(name, url) values
<foreach collection="list" item="item" index="index"
separator=",">
(#{item.name}, #{item.url})
</foreach>
</insert>
<delete id="delete4batch" parameterType="List">
delete from shop_meta where
<foreach collection="list" item="item" index="index"
separator="or">id = #{item.id}</foreach>
</delete>
<select id="update4authorize_init" parameterType="com.silvery.project.cms.vo.AdminMetaVo"
resultType="com.silvery.project.cms.model.AdminRole">
select * from shop_role where id
<if test="authorize == 1">
in
</if>
<if test="authorize == 2">
not in
</if>
(select role_id from shop_role_meta where meta_id = #{id})
</select>
<update id="update4authorize" parameterType="com.silvery.project.cms.vo.AdminMetaVo">
<if test="authorize == 1">
insert into shop_role_meta(meta_id, role_id) values(#{id}, #{roleId})
</if>
<if test="authorize == 2">
delete from shop_role_meta where role_id = #{roleId} and meta_id = #{id}
</if>
</update>
<select id="query4roles" parameterType="com.silvery.project.cms.vo.AdminMetaVo"
resultType="com.silvery.project.cms.model.AdminRole">
select * from shop_role where id in (select role_id from shop_role_meta where meta_id = #{id})
</select>
</mapper>
完毕。