ssm纯注解配置文件配置例子
描述
applicationContext.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
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/tx http://www.springframework.org/schema/tx/spring-tx-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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
default-autowire="byName">
<description>Spring公共配置</description>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<context:component-scan base-package="project">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<bean name="springApplicationContextHolder" class="project.util.SpringApplicationContextHolder"></bean>
<!-- 数据源配置 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 需要修改的配置地方 mysql -->
<property name="url" value="jdbc:mysql://localhost:3306/javawebxx_blog?characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="filters" value="stat" />
<property name="maxActive" value="80" />
<property name="initialSize" value="1" />
<property name="maxWait" value="60000" />
<property name="minIdle" value="1" />
<property name="timeBetweenEvictionRunsMillis" value="3000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 'x' FROM DUAL" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<!-- jdbc事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<!--事务模板 -->
<bean id="transactionTemplate"
class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<!--ISOLATION_DEFAULT 表示由使用的数据库决定 -->
<property name="isolationLevelName" value="ISOLATION_DEFAULT"/>
<property name="propagationBehaviorName" value="PROPAGATION_REQUIRED"/>
<!-- <property name="timeout" value="30"/> -->
</bean>
<!-- db操作类 -->
<bean id="db"
class="project.util.DBHelper">
<property name="jdbcTemplate">
<ref local="jdbcTemplate" />
</property>
<property name="transactionTemplate">
<ref local="transactionTemplate" />
</property>
</bean>
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:project/model/mapping/*.xml" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<!-- 注入到dao层 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="project.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
一个controller配置例子:
package project.controller.admin;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Date;
import project.controller.MyController;
import project.model.Blog;
import project.service.BlogService;
import project.controller.MyController;
/**
*博客文章
* @author Administratorxxxx
* @date2019-01-11
*/
/**
* @author https://www.javawebxx.com javaweb学习网
* 备注:>ssm框架ssh框架springmvc框架搭建下载javaweb学习网
*/
@Controller("blogController")
@RequestMapping(value = "/admin/blog")
public class BlogController extends MyController {
@Autowired
BlogService blogService;
/**
* 查询frame
*/
@RequestMapping(value = "/frame")
public String frame(Model model, HttpServletRequest request,String flag)throws Exception {
return "/admin/blog/frame";
}
/**
* 查询列表
*/
@RequestMapping(value = "/list")
public String list(Model model, HttpServletRequest request,String flag,String title,Blog blog)throws Exception {
List list = blogService.getBlogList(request, blog);
request.setAttribute("list", list);
return "/admin/blog/list";
}
/**
* 编辑保存(包含修改和添加)
*/
@RequestMapping(value = "/editSave")
public ResponseEntity editSave(Model model,HttpServletRequest request,Long id,String flag,Blog blog
) throws Exception{
int result = 0;
if(id!=null){
result = blogService.update(request, blog);
}else{
result = blogService.save(request, blog);
}
if(result==1){
return renderData(true,"操作成功",null);
}else{
return renderData(false,"操作失败",null);
}
}
/**
* 删除信息
*/
@RequestMapping(value = "/editDelete")
public ResponseEntity editDelete(Model model,HttpServletRequest request,Long id,String flag) throws Exception {
int result = blogService.deleteOne(request, id);
if(result==1){
return renderData(true,"操作成功",null);
}else{
return renderData(false,"操作失败",null);
}
}
/**
* 跳转到编辑页面
*/
@RequestMapping(value = "/edit")
public String edit(Model model, HttpServletRequest request,Long id,String flag)throws Exception {
if(id!=null){
//修改
Blog blog = blogService.getBlog(request, id);
model.addAttribute("map", blog);
}String sql="select * from t_fenlei";
List flList = db.queryForList(sql);
model.addAttribute("flList", flList);
return "/admin/blog/edit";
}
}
一个ssm的mapping配置例子:
<?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="project.dao.BlogDao">
<resultMap id="BaseResultMap" type="project.model.Blog">
<id column="title" property="title"/>
<id column="headPic" property="headPic"/>
<id column="content" property="content"/>
<id column="showDate" property="showDate"/>
<id column="status" property="status"/>
<id column="px" property="px"/>
<id column="abc" property="abc"/>
<id column="types" property="types"/>
<id column="cNum" property="cNum"/>
<id column="pNum" property="pNum"/>
<id column="istj" property="istj"/>
</resultMap>
<insert id="insert">
insert into t_blog(title,headPic,content,showDate,status,px,abc,types,cNum,pNum,istj) values(#{title},#{headPic},#{content},#{showDate},#{status},#{px},#{abc},#{types},#{cNum},#{pNum},#{istj})
</insert>
<update id="update">
update t_blog
<trim prefix="set" suffixOverrides=",">
<if test="title!=null">title=#{title},</if>
<if test="headPic!=null">headPic=#{headPic},</if>
<if test="content!=null">content=#{content},</if>
<if test="showDate!=null">showDate=#{showDate},</if>
<if test="status!=null">status=#{status},</if>
<if test="px!=null">px=#{px},</if>
<if test="abc!=null">abc=#{abc},</if>
<if test="types!=null">types=#{types},</if>
<if test="cNum!=null">cNum=#{cNum},</if>
<if test="pNum!=null">pNum=#{pNum},</if>
<if test="istj!=null">istj=#{istj},</if>
</trim>
where id=#{id}
</update>
<select id="getById" resultMap="BaseResultMap"
parameterType="project.model.Blog">
select * from t_blog where id=#{id}
</select>
<delete id="delete">
delete from t_blog where id=#{id}
</delete>
<select id="queryForList" resultMap="BaseResultMap" parameterType="project.model.Blog">
select * from t_blog where 1=1
<if test="title!=null and title!=''">
and title like concat(concat('%',#{title}),'%')
</if>
<if test="begin!=null and page_num!=null">limit #{begin},#{page_num}</if>
</select>
<select id="countAll" resultType="java.lang.Integer" parameterType="project.model.Blog">
select count(1) from t_blog where 1=1
<if test="title!=null">
and title like concat(concat('%',#{title}),'%')
</if>
</select>
</mapper>
javawebxx.com由javaweb学习网所有 网站地图 备案号:苏ICP备17055254号-1
留言