Spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<context:property-placeholder location="classpath:db.properties"/>
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="${jdbc.jdbcUrl}"></property>
<property name="driverClassName" value="${jdbc.driverClass}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- Mybatis的工厂 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 核心配置文件的位置 -->
<property name="configLocation" value="classpath:/sqlMapConfig.xml"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定一个基本包 :会直接包含包下的所有mapper-->
<property name="basePackage" value="com.rabbit.crm.mapper"></property>
</bean>
<!-- 注解事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
Mybatis 配置文件
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.rabbit.crm.mapper.CustomerDao">
<!-- //总条数结果集
public Integer customerCountByQueryVo(QueryVo vo);
//结果集
public List<Customer> selectCoustomerListByQueryVo(QueryVo vo); -->
<select id="customerCountByQueryVo" parameterType="QueryVo" resultType="Integer">
select count(1) from customer
<where>
<if test="custName != null and custName != '' ">
cust_name like "%" #{custName}"%"
</if>
<if test="custSource != null and custSource != '' ">
and cust_source like "%" #{custSource}"%"
</if>
<if test="custIndustry != null and custIndustry != '' ">
and cust_industry like "%" #{custIndustry}"%"
</if>
<if test="custLevel != null and custLevel != '' ">
and cust_level like "%" #{custLevel}"%"
</if>
</where>
</select>
<select id="selectCoustomerListByQueryVo" parameterType="QueryVo" resultType="Customer">
select * from customer
<where>
<if test="custName != null and custName != '' ">
and cust_name like "%" #{custName}"%"
</if>
<if test="custSource != null and custSource != '' ">
and cust_source like "%" #{custSource}"%"
</if>
<if test="custIndustry != null and custIndustry != '' ">
and cust_industry like "%" #{custIndustry}"%"
</if>
<if test="custLevel != null and custLevel != '' ">
and cust_level like "%" #{custLevel}"%"
</if>
</where>
limit #{startRow},#{size}
</select>
<!-- //通过Id查询客户
public Customer selectById(Integer id); -->
<select id="selectById" parameterType="Integer" resultType="Customer">
select * from customer where cust_id = #{id}
</select>
<!-- //修改客户通过ID
public void updateCustomerById(Customer customer); -->
<update id="updateCustomerById" parameterType="Customer">
update customer
<set>
<if test="cust_name != null">
cust_name = #{cust_name},
</if>
<if test="cust_source != null">
cust_source = #{cust_source},
</if>
<if test="cust_industry != null">
cust_industry = #{cust_industry},
</if>
<if test="cust_level != null">
cust_level = #{cust_level},
</if>
<if test="cust_linkman != null">
cust_linkman = #{cust_linkman},
</if>
<if test="cust_phone != null">
cust_phone = #{cust_phone},
</if>
<if test="cust_mobile != null">
cust_mobile = #{cust_mobile},
</if>
<if test="cust_zipcode != null">
cust_zipcode = #{cust_zipcode},
</if>
<if test="cust_address != null">
cust_address = #{cust_address}
</if>
</set>
<where>
cust_id = #{cust_id}
</where>
</update>
<!-- //通过ID删除客户
public void deleteCustomerById(Integer id); -->
<delete id="deleteCustomerById" parameterType="Integer">
delete from customer
<where>
cust_id = #{id}
</where>
</delete>
</mapper>
主配置文件:
<?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>
<!-- 配置别名 -->
<typeAliases>
<!-- 配置包路径,在mapper.xml文件下com.rabbit.bean这个路径下的所有类都可以直接写类名 且首字母大小写都可以 -->
<package name="com.rabbit.crm.pojo"/>
</typeAliases>
</configuration>
Spring Mvc配置文件
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd ">
<!-- 扫描@Contorller @Service -->
<context:component-scan base-package="com.rabbit"></context:component-scan>
<context:property-placeholder location="classpath:resource.properties"/>
<!-- 处理器映射器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> -->
<!-- 处理器适配器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->
<!-- 注解驱动 相当于 处理器适配器跟处理器映射器-->
<mvc:annotation-driven/>
<!-- 视图解释器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 对静态资源放行 -->
<mvc:resources location="/js/" mapping="/js/*"/>
<mvc:resources location="/css/" mapping="/css/*"/>
<mvc:resources location="/fonts/" mapping="/fonts/*"/>
</beans>
Service和Controller层的注解配置
Service:注意类上加@Service
/**
* 客户管理
* @author rabbit
*
*/
@Service
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerDao customerDao;
@Override
public Page<Customer> selectPageByQueryVo(QueryVo vo) {
Page<Customer> page = new Page<>();
//每页显示条数
page.setSize(5);
vo.setSize(5);
//判断当前页
if(null != vo ) {
if(null != vo.getPage()) {
page.setPage(vo.getPage());
vo.setStartRow((vo.getPage()-1)*vo.getSize());
}
if(null != vo.getCustName() && !"".equals(vo.getCustName().trim())) {
vo.setCustName(vo.getCustName().trim());;
}
if(null != vo.getCustSource() && !"".equals(vo.getCustSource().trim())) {
vo.setCustSource(vo.getCustSource().trim());;
}
if(null != vo.getCustIndustry() && !"".equals(vo.getCustIndustry().trim())) {
vo.setCustIndustry(vo.getCustIndustry().trim());;
}
if(null != vo.getCustLevel() && !"".equals(vo.getCustLevel().trim())) {
vo.setCustLevel(vo.getCustLevel().trim());;
}
//总条数
page.setTotal(customerDao.customerCountByQueryVo(vo));
page.setRows(customerDao.selectCoustomerListByQueryVo(vo));
}
return page;
}
@Override
public Customer selectById(Integer id) {
return customerDao.selectById(id);
}
@Override
public void updateCustomerById(Customer customer) {
customerDao.updateCustomerById(customer);
}
@Override
public void deleteCustomerById(Integer id) {
customerDao.deleteCustomerById(id);
}
}
Controller:
@Controller
public class CustomerContoller {
@Autowired
private BasedictService baseDictService;
@Autowired
private CustomerService customerService;
//注入在成员变量
@Value("${type_code2}")
private String type_code2;
@Value("${type_code1}")
private String type_code1;
@Value("${type_code6}")
private String type_code6;
@RequestMapping(value = "/customer/list.action")
public String list(QueryVo vo,Model model) {
List<BaseDict> fromType = baseDictService.selectByCodes(type_code2);
List<BaseDict> industryType = baseDictService.selectByCodes(type_code1);
List<BaseDict> levelType = baseDictService.selectByCodes(type_code6);
model.addAttribute("fromType",fromType);
model.addAttribute("industryType",industryType);
model.addAttribute("levelType",levelType);
//通过4个条件查询分页对象
Page<Customer> page = customerService.selectPageByQueryVo(vo);
model.addAttribute("page",page);
return "customer";
}
//去修改页面
@RequestMapping(value = "/customer/edit.action")
public @ResponseBody
Customer edit(Integer id) {
return customerService.selectById(id);
}
//修改保存customer/update.action
@RequestMapping(value = "/customer/update.action")
public @ResponseBody String update(Customer customer) {
customerService.updateCustomerById(customer);
return "OK";
}
//删除客户
@RequestMapping(value = "/customer/delete.action")
public @ResponseBody String delete(Integer id) {
customerService.deleteCustomerById(id);
return "OK";
}
}