标签:宝宝 conf exec tps config aop dbcp data 映射
本宝宝菜鸟一枚,这个项目献给还在学的朋友们,第一次写,写的不好不要介意。好了直接上一个以前写的小demo,很简单,也很好理解直接copy出去改改就能运行了,包需要自己见,架包需要自己导,页面自己写个简单的测试一下就能跑起来了,,献丑了!
db.properties文件:数据库用的是mysql
dbcp.driver=com.mysql.jdbc.Driver
dbcp.validationQuery=SELECT 1
dbcp.url=jdbc:mysql://localhost:3306/这里写你的数据库名字?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
dbcp.username=root
dbcp.password=root
dbcp.init=1
dbcp.max=5
dbcp.min=2
dbcp.defaultAutoCommit=true
dbcp.test_sql=select 1 from dual
最简单的mybatis.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>
<!-- setttings属性设置 -->
<settings>
<!-- 全局映射器启用缓存 -->
<setting name="cacheEnabled" value="true" />
<!-- 查询时, 关闭关联对象即时加载以提高性能 -->
<setting name="lazyLoadingEnabled" value="true" />
<!-- 设置关联对象加载的形态, 此处为按需加载字段(加载字段由 SQL指 定) , 不会加载关联表的所有字段, 以提高性能 -->
<setting name="aggressiveLazyLoading" value="true" />
<!-- 对于未知的 SQL 查询, 允许返回不同的结果集以达到通用的效果 -->
<setting name="multipleResultSetsEnabled" value="true" />
<!-- 允许使用列标签代替列名 -->
<setting name="useColumnLabel" value="true" />
<!-- 允许使用自定义的主键值(比如由程序生成的 UUID 32 位编码作为 键值) , 数据表的 PK 生成策略将被覆盖 -->
<setting name="useGeneratedKeys" value="true" />
<!-- 给予被嵌套的 resultMap 以字段-属性的映射支持 -->
<setting name="autoMappingBehavior" value="FULL" />
<!-- 对于批量更新操作缓存 SQL 以提高性能 -->
<setting name="defaultExecutorType" value="BATCH" />
<!-- 数据库超过 25000 秒仍未响应则超时 -->
<setting name="defaultStatementTimeout" value="25000" />
</settings>
<!-- alias -->
<typeAliases>
<typeAlias alias="user" type="这里是所要写的模型bean"></typeAlias>
</typeAliases>
</configuration>
这里是最简单的spring.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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 这里写你所需要扫描控制层包,类不写 -->
<context:component-scan base-package="com.testing">
<!-- 包下扫描类的时候,不扫描Controller注解的类 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 引入资源文件 ,里面配置的是数据库连接的几大要素-->
<context:property-placeholder location="classpath:db.properties" />
<!-- 数据库连接池 -->
<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${dbcp.driver}"></property>
<property name="url" value="${dbcp.url}"></property>
<property name="username" value="${dbcp.username}"></property>
<property name="password" value="${dbcp.password}"></property>
<property name="initialSize" value="${dbcp.init}"></property>
<property name="maxIdle" value="${dbcp.max}"></property>
<property name="minIdle" value="${dbcp.min}"></property>
<property name="defaultAutoCommit" value="${dbcp.defaultAutoCommit}"></property>
<property name="validationQuery" value="${dbcp.test_sql}"></property>
</bean>
<!-- mybatis的sessionFactory -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 关联mybatis主配置文件 -->
<property name="configLocation" value="classpath:mybatis.xml"></property>
<!-- 关联映射sql文件 -->
<property name="mapperLocations" value="classpath:com/testing/mapper/*.xml"></property>
</bean>
<!-- mybatis的SqlSessionTemplate -->
<bean id="session" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sessionFactory"></constructor-arg>
</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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- -扫描只包含controller控制器层- -->
<context:component-scan base-package="com.testing">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:include-filter>
</context:component-scan>
</beans>
剩下的就是包了:主要是模型bean层 控制层controller的包 主要放映射文件的包 还有一个是service包
这是项目结构:
lib:
bean层里面的就不写了很简单,这里主要写一下服务层service和mapper映射文件还有controller层:
mapper映射文件:
<?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.slee">
<select id="select" resultType="slee">
select * from sleeParams
</select>
<delete id="deleteById" parameterType="slee">
delete from sleeParams where
<if test="sleeParamsId!=null and sleeParamsId !=‘‘">
sleeParamsId=#{sleeParamsId}
</if>
</delete>
<update id="update" parameterType="slee">
update sleeParams set
sleeParamsJx=#{sleeParamsJx}
,sleeParamsPcbType=#{sleeParamsPcbType}
,sleeParamsEMS=#{sleeParamsEMS}
,sleeParamscode=#{sleeParamscode}
,sleeParamsBBH=#{sleeParamsBBH}
,sleeParamsMarkeCode=#{sleeParamsMarkeCode}
,sleeParamsStatus=#{sleeParamsStatus}
where sleeParamsId=#{sleeParamsId}
</update>
<insert id="insertSlee" parameterType="slee"
useGeneratedKeys="true" keyProperty="sleeParamsId">
insert into sleeParams
(sleeParamsId,
sleeParamsJx,
sleeParamsPcbType,
sleeParamsEMS,
sleeParamscode,
sleeParamsBBH,
sleeParamsMarkeCode)
values
(#{sleeParamsId},
#{sleeParamsJx},
#{sleeParamsPcbType},
#{sleeParamsEMS},
#{sleeParamscode},
#{sleeParamsBBH},
#{sleeParamsMarkeCode}
)
</insert>
<select id="selectById" resultType="slee" parameterType="slee">
select * from sleeParams where sleeParamsId=#{sleeParamsId}
</select>
</mapper>
服务层service:
package com.testing.service;
import java.util.List;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.testing.bean.SleeParamsBean;
@Service
public class SleeService {
@Autowired
private SqlSessionTemplate session;
public boolean add(SleeParamsBean bean){
try {
session.insert("com.slee.insertSlee", bean);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public List<SleeParamsBean> query( SleeParamsBean bean){
return session.selectList("com.slee.select",bean );
}
public boolean delete(SleeParamsBean bean){
try {
session.delete("com.slee.deleteById", bean);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public boolean update(SleeParamsBean bean){
try {
session.update("com.slee.update", bean);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public List<SleeParamsBean> find(SleeParamsBean bean){
return session.selectList("com.slee.selectById", bean.getSleeParamsId());
}
}
控制层controller:
package com.testing.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.testing.bean.SleeParamsBean;
import com.testing.service.SleeService;
@Controller
public class SleeParamsController {
private Log log = LogFactory.getLog(SleeParamsController.class);
@Autowired
private SleeService service;
/*
* 查询
*/
@RequestMapping(value="/querySlee")
public String query(HttpServletRequest request){
log.info("进入查询controller层==============");
SleeParamsBean bean = new SleeParamsBean();
List list = service.query(bean);
request.setAttribute("querylist", list);
return "querySlee.jsp";
}
/*
* 添加
*/
@RequestMapping(value="addSlee",method=RequestMethod.POST)
public String add(SleeParamsBean bean,HttpServletRequest request){
log.info("进入添加的controller层");
boolean flag = service.add(bean);
if (flag) {
request.setAttribute("message","添加成功!");
}else {
request.setAttribute("message", "添加失败!");
}
return "addSlee.jsp";
}
/*
* 查询单个
*/
@RequestMapping(value="findSlee")
public String find(HttpServletRequest request,SleeParamsBean bean){
log.info("进入查询单个数据controller层");
SleeParamsBean bean2 = service.find(bean).get(0);
request.setAttribute("slee",bean2 );
return "updateSlee.jsp";
}
@RequestMapping(value="updateSlee",method=RequestMethod.POST)
public String update(SleeParamsBean bean , HttpServletRequest request){
log.info("进入修改的controller层");
boolean flag = service.update(bean);
if (flag) {
request.setAttribute("message", "修改成功");
}else {
request.setAttribute("message", "修改失败");
}
return "querySlee.jsp";
}
/*
* 删除
*/
@RequestMapping(value="deleteSlee")
public String delete(HttpServletRequest request,SleeParamsBean bean){
log.info("进入删除的页面");
boolean flag = service.delete(bean);
if (flag) {
request.setAttribute("message", "删除成功");
}else{
request.setAttribute("message", "删除失败");
}
return "querySlee.jsp";
}
}
最简单的Spring+SpringMVC+Mybatis的整合
标签:宝宝 conf exec tps config aop dbcp data 映射
原文地址:http://www.cnblogs.com/zlazm/p/7892629.html