SSM项目3_框架练习

环境准备

1.配置SqlMapConfig.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>
	<typeAliases>
		<package name="cn.itcast.crm.pojo"/>
	</typeAliases>
</configuration>

2.配置applicationContext-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	<!-- 配置 读取properties文件 jdbc.properties -->
	<context:property-placeholder location="classpath:db.properties" />

	<!-- 配置 数据源 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<!-- 配置SqlSessionFactory -->
	<bean class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 设置MyBatis核心配置文件 -->
		<property name="configLocation" value="classpath:SqlMapConfig.xml" />
		<!-- 设置数据源 -->
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 配置Mapper扫描 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 设置Mapper扫描包 -->
		<property name="basePackage" value="cn.itcast.crm.mapper" />
	</bean>
</beans>

3.配置db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/crm?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

4.配置log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

5.配置applicationContext-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	<!-- 配置Service扫描 -->
	<context:component-scan base-package="cn.itcast.crm.service" />
</beans>

6.配置applicationContext-trans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	<!-- 事务管理器 -->
	<bean id="transactionManager"	class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 传播行为 -->
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="create*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="query*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<!-- 切面 -->
	<aop:config>
		<aop:advisor advice-ref="txAdvice"
			pointcut="execution(* cn.itcast.crm.service.*.*(..))" />
	</aop:config>

</beans>

7.配置springmvc.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: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.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<!-- 配置Controller扫描 -->
	<context:component-scan base-package="cn.itcast.crm.controller" />

	<!-- 配置注解驱动 -->
	<mvc:annotation-driven />
	
	<!-- 对静态资源放行  /**是为了防止下面还有文件夹-->
	<mvc:resources location="/css/" mapping="/css/**"/>
	<mvc:resources location="/js/" mapping="/js/**"/>
	<mvc:resources location="/fonts/" mapping="/fonts/**"/>

	<!-- 配置视图解析器 -->
	<bean	class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 前缀 -->
		<property name="prefix" value="/WEB-INF/jsp/" />
		<!-- 后缀 -->
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

8.配置web.xml

 <!-- 配置spring -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext-*.xml</param-value>
	</context-param>

	<!-- 配置监听器加载spring -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- 配置过滤器,解决post的乱码问题 -->
	<filter>
		<filter-name>encoding</filter-name>	
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- 配置SpringMVC -->
	<servlet>
		<servlet-name>ssm_base</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<!-- 配置springmvc什么时候启动,参数必须为整数 -->
		<!-- 如果为0或者大于0,则springMVC随着容器启动而启动 -->
		<!-- 如果小于0,则在第一次请求进来的时候启动 -->
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>ssm_base</servlet-name>
		<!-- 所有的请求都进入springMVC -->
		<url-pattern>/</url-pattern>
	</servlet-mapping>

9.加入静态资源

常识:在springmvc中如何解决硬编码问题

首先写一个配置文件 type.properties

#客户来源
CUSTOMER_FROM_TYPE=002
#客户行业
CUSTOMER_INDUSTRY_TYPE=001
#客户级别
CUSTOMER_LEVEL_TYPE=006

在springmvc.xml中加载type.properties

<!-- 加载controller需要的配置信息 -->
	<context:property-placeholder location="classpath:type.properties" />

记得将页面的form提交方式改成post

一:CRM去客户管理列表页面

二:实现查询条件初始化(将这些数据加载进来)

在查询客户的时候,可以选择客户来源,所属行业,客户级别信息,页面加载时需要初始化查询条件下拉列表。

三:客户管理列表之分页对象

四:去修改用户信息 (回显功能实现)

五:修改客户数据

六:根据id删除用户信息

一:

@Controller
@RequestMapping("/customer")
public class CustomerController {
	
	@RequestMapping("/list")
	public String customerList(){
		return "customer";
	}

}

二:

@Controller
@RequestMapping("customer")
public class CustomerController {
	
	@Autowired
	private BaseDictService baseDictService;
	
		// 客户来源
		@Value("${CUSTOMER_FROM_TYPE}")
		private String CUSTOMER_FROM_TYPE;
		// 客户行业
		@Value("${CUSTOMER_INDUSTRY_TYPE}")
		private String CUSTOMER_INDUSTRY_TYPE;
		// 客户级别
		@Value("${CUSTOMER_LEVEL_TYPE}")
		private String CUSTOMER_LEVEL_TYPE;
	
	//显示客户列表
	@RequestMapping("list")
	public String customerList(Model model){
		// 客户来源
		List<BaseDict> fromType  = baseDictService.queryBaseDictByDictTypeCode(this.CUSTOMER_FROM_TYPE);
		// 所属行业
		List<BaseDict> industryType  = baseDictService.queryBaseDictByDictTypeCode(this.CUSTOMER_INDUSTRY_TYPE);
		// 客户级别
		List<BaseDict> levelType  = baseDictService.queryBaseDictByDictTypeCode(this.CUSTOMER_LEVEL_TYPE);
		model.addAttribute("fromType", fromType);
		model.addAttribute("industryType", industryType);
		model.addAttribute("levelType", levelType);
		return "customer";
	}

}


@Service
public class BaseDictServiceImpl implements BaseDictService {
	@Autowired
	private BaseDictMapper baseDictMapper;

	public List<BaseDict> queryBaseDictByDictTypeCode(String dictTypeCode) {
		return baseDictMapper.queryBaseDictByDictTypeCode(dictTypeCode);
	}
}

public interface BaseDictMapper {
//	根据类别代码查询数据
	public List<BaseDict> queryBaseDictByDictTypeCode(String dictTypeCode);
}

<mapper namespace="cn.itcast.crm.mapper.BaseDictMapper">
	<select id="queryBaseDictByDictTypeCode" parameterType="String" resultType="BaseDict">
		select * from base_dict where dict_type_code=#{value} 
	</select>
</mapper>

三:

public class QueryVo {

	private String custName;
	private String custSource;
	private String custIndustry;
	private String custLevel;

	// 当前页码数
	private Integer page = 1;
	// 数据库从哪一条数据开始查  索引
	private Integer start;
	// 每页显示数据条数
	private Integer rows = 10;
get/set。。。。。。
}


public interface CustomerMapper {
	//根据queryVo分页查询数据
	List<Customer> queryCustomerByQueryVo(QueryVo queryVo);
	//根据queryVo查询数据条数
	int queryCountByQueryVo(QueryVo queryVo);
}


<mapper namespace="cn.itcast.crm.mapper.CustomerMapper">
	<sql id="customerQueryVo">
		<where>
			<if test="custName != null and custName != ''">
				AND a.cust_name LIKE "%"#{custName}"%"
			</if>
			<if test="custSource != null and custSource != ''">
				AND a.cust_source = #{custSource}
			</if>
			<if test="custIndustry != null and custIndustry != ''">
				AND a.cust_industry = #{custIndustry}
			</if>
			<if test="custLevel != null and custLevel != ''">
				AND a.cust_level = #{custLevel}
			</if>
		</where>
	</sql>

	<!-- 根据queryVo分页查询数据 -->
	<select id="queryCustomerByQueryVo" parameterType="QueryVo" resultType="Customer">
		SELECT
			a.cust_id,
			a.cust_name,
			a.cust_user_id,
			a.cust_create_id,
			b.dict_item_name cust_source,
			c.dict_item_name cust_industry,
			d.dict_item_name cust_level,
			a.cust_linkman,
			a.cust_phone,
			a.cust_mobile,
			a.cust_zipcode,
			a.cust_address,
			a.cust_createtime
		FROM
			customer a
		LEFT JOIN base_dict b ON a.cust_source = b.dict_id
		LEFT JOIN base_dict c ON a.cust_industry = c.dict_id
		LEFT JOIN base_dict d ON a.cust_level = d.dict_id
		<include refid="customerQueryVo" />
		<if test="start != null and rows != null">
			LIMIT #{start}, #{rows}
		</if>
	</select>

	<!-- 根据queryVo查询数据条数 -->
	<select id="queryCountByQueryVo" parameterType="QueryVo" resultType="int">
		SELECT count(1)  FROM  customer a
		LEFT JOIN base_dict b ON a.cust_source = b.dict_id
		LEFT JOIN base_dict c ON a.cust_industry = c.dict_id
		LEFT JOIN base_dict d ON a.cust_level = d.dict_id
		<include refid="customerQueryVo" />
                <if test="start != null and rows != null">
			LIMIT #{start}, #{rows}
		</if>
	</select>
</mapper>

public interface CustomerService {
//	根据条件分页查询客户
	Page<Customer> queryCustomerByQueryVo(QueryVo queryVo);
}

@Service
public class CustomerServiceImpl implements CustomerService {
	@Autowired
	private CustomerMapper customerMapper;

	@Override
	public Page<Customer> queryCustomerByQueryVo(QueryVo queryVo) {
		// 设置查询条件,从哪一条数据开始查
		queryVo.setStart((queryVo.getPage() - 1) * queryVo.getRows());
		// 查询数据结果集
		List<Customer> list = customerMapper.queryCustomerByQueryVo(queryVo);
		// 查询到的数据总条数
		int total = customerMapper.queryCountByQueryVo(queryVo);
		Page<Customer> page=new Page();
		//数据的总条数
		page.setTotal(total);
		//当前页码数
		page.setSize(queryVo.getPage());
		//每页显示的数据条数
		page.setSize(queryVo.getRows());
		//每页的数据
		page.setRows(list);
		return page;
	}
}

@Controller
@RequestMapping("customer")
public class CustomerController {
	
	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private BaseDictService baseDictService;
	// 客户来源
	@Value("${CUSTOMER_FROM_TYPE}")
	private String CUSTOMER_FROM_TYPE;
	// 客户行业
	@Value("${CUSTOMER_INDUSTRY_TYPE}")
	private String CUSTOMER_INDUSTRY_TYPE;
	// 客户级别
	@Value("${CUSTOMER_LEVEL_TYPE}")
	private String CUSTOMER_LEVEL_TYPE;
	
	@Autowired
	private BaseDictService type;
	
	//显示客户列表
	@RequestMapping("list")
	public String customerList(Model model,QueryVo queryVo){
		// 客户来源
		List<BaseDict> fromType  = baseDictService.queryBaseDictByDictTypeCode(this.CUSTOMER_FROM_TYPE);
		// 所属行业
		List<BaseDict> industryType  = baseDictService.queryBaseDictByDictTypeCode(this.CUSTOMER_INDUSTRY_TYPE);
		// 客户级别
		List<BaseDict> levelType  = baseDictService.queryBaseDictByDictTypeCode(this.CUSTOMER_LEVEL_TYPE);
		model.addAttribute("fromType", fromType);
		model.addAttribute("industryType", industryType);
		model.addAttribute("levelType", levelType);
		Page<Customer> page = customerService.queryCustomerByQueryVo(queryVo);
		model.addAttribute("page", page);
		//参数回显
		model.addAttribute("custName",queryVo.getCustName());
		model.addAttribute("custSource",queryVo.getCustSource());
		model.addAttribute("custIndustry",queryVo.getCustIndustry());
		model.addAttribute("custLevel",queryVo.getCustLevel());
		return "customer";
	}
}

四:

//根据id查询客户
Customer queryCustomerById(Long id);

<!-- 根据id查询用户 -->
<select id="queryCustomerById" resultType="cn.itcast.crm.pojo.Customer">
	SELECT * FROM customer WHERE cust_id = #{id}
</select>

public Customer queryCustomerById(Long id) {
		return customerMapper.queryCustomerById(id);
	}

//根据id查询用户,返回json格式数据
@RequestMapping("edit")
@ResponseBody
public Customer queryCustomerById(Long id) {
	Customer customer = this.customerService.queryCustomerById(id);
	return customer;
}

五:   <set>去掉最后一个,注意这个#{cust_id}是这个对象的主键值id

//根据id编辑客户
	void updateCustomerById(Customer customer);

<update id="updateCustomerById" parameterType="Customer" >
		UPDATE customer
		<set>
			<if test="cust_name !=null and cust_name != ''">
				cust_name= #{cust_name},
			</if>
			<if test="cust_source !=null and cust_source != ''">
				cust_source = #{cust_source},
			</if>
		</set>
		<where>
		cust_id = #{cust_id};
		</where>
</update>


public void updateCustomerById(Customer customer) {
	customerMapper.updateCustomerById(customer);
}


//$.post返回默认只认识ok
@RequestMapping("update")
@ResponseBody
public String update(Customer customer){
   customerService.updateCustomerById(customer);
   return "ok";
}

function deleteCustomer(id) {
			if(confirm('确实要删除该客户吗?')) {
				$.post("<%=basePath%>customer/delete.action",{"id":id},function(data){
					alert("客户删除更新成功!");
					window.location.reload();
				});
			}
		}

六:

void deleteCustomerById(Long id);
<delete id="deleteCustomerById" parameterType="long">
	delete from customer where cust_id=#{cust_id}
</delete>

public void deleteCustomerById(Long id) {
	customerMapper.deleteCustomerById(id);
}

@RequestMapping("delete")
@ResponseBody
public String delete(Long id){
	customerService.deleteCustomerById(id);
	return "ok";
}

function deleteCustomer(id) {
   if(confirm('确实要删除该客户吗?')) {
			$.post("<%=basePath%>customer/delete.action",{"id":id},function(data){
			alert("客户删除成功!");
			window.location.reload();
		});
	}
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

guoyebing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值