Mybatis、Springmvc练习

数据库

工程搭建

工程使用Springmvc、spring、mybatis框架整合完成。

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>

</configuration>

 

applicationContext-dao.xml

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

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

	<!-- 配置 Mybatis的工厂 -->
	<bean class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 配置Mybatis的核心 配置文件所在位置 -->
		<property name="configLocation" value="classpath:SqlMapConfig.xml" />
		<!-- 配置pojo别名 -->
		<property name="typeAliasesPackage" value="com.itheima.boot.pojo"></property>
	</bean>

	<!-- 配置 1:原始Dao开发 接口实现类 Mapper.xml 三个 2:接口开发 接口 不写实现类 Mapper.xml 二个 (UserDao、ProductDao 
		、BrandDao。。。。。。。) 3:接口开发、并支持扫描 cn.itcast.core.dao(UserDao。。。。。) 写在此包下即可被扫描到 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.itheima.boot.dao" />
	</bean>

 

applicationContext-service.xml

	<!-- 配置  扫描   @Service -->
	<context:component-scan base-package="com.itheima.boot.service"/>

 

applicationContext-trans.xml

<!-- 事务管理器 -->
	<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:attributes>
	</tx:advice>
	<!-- 切面 -->
	<aop:config>
		<aop:advisor advice-ref="txAdvice"
			pointcut="execution(* com.itheima.boot.service.*.*(..))" />
	</aop:config>

 

Springmvc.xml

	<!-- 配置 读取properties文件 jdbc.properties -->
	<context:property-placeholder location="classpath:resource.properties" />
	
	<context:component-scan base-package="com.itheima.boot.controller" />
	<!-- 配置处理器映射器 适配器 -->
	<mvc:annotation-driven />
	<!-- 配置视图解释器 jsp -->
	<bean id="jspViewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>

 

Web.xml

<display-name>boot-crm</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<!-- 上下文的位置 -->
	<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提交过滤器 UTF-8 -->
	<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>*.action</url-pattern>
	</filter-mapping>
	<!-- 前端控制器 -->
	<servlet>
		<servlet-name>crm</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<!-- 此处不配置 默认找 /WEB-INF/[servlet-name]-servlet.xml -->
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>crm</servlet-name>
		<!-- 1:*.do *.action 拦截以.do结尾的请求 (不拦截 jsp png jpg .js .css) 2:/ 拦截所有请求 
			(不拦截.jsp) 建议使用此种 方式 (拦截 .js.css .png) (放行静态资源) 3:/* 拦截所有请求(包括.jsp) 此种方式 不建议使用 -->
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>

 

加入jsp及分页标签  JSP地址 密码:opig

编写controller层

	@RequestMapping("/customer/list")
	public String showCustomerList(QueryVo queryVo,Model model) throws Exception {
		//乱码处理
		if (StringUtils.isNotBlank(queryVo.getCustName())) {
			queryVo.setCustName(new String(queryVo.getCustName().getBytes("iso8859-1"), "utf-8"));
		}
		//初始化客户来源
		List<BaseDict> sourceList = baseDictService.getDictListByTypeCode(custSorceCode);
		//所属行业
		List<BaseDict> industoryList = baseDictService.getDictListByTypeCode(custIndustoryCode);
		//客户级别
		List<BaseDict> levelList = baseDictService.getDictListByTypeCode(custLevelCode);
		//把字典信息传递给页面
		model.addAttribute("fromType", sourceList);
		model.addAttribute("industryType", industoryList);
		model.addAttribute("levelType", levelList);
		//根据查询条件查询客户列表
		Page<Customer> page = customerService.getCustomerList(queryVo);
		//把客户列表传递给页面
		model.addAttribute("page", page);
		//参数回显
		model.addAttribute("custName", queryVo.getCustName());
		model.addAttribute("custSource", queryVo.getCustSource());
		model.addAttribute("custIndustry", queryVo.getCustIndustory());
		model.addAttribute("custLevel", queryVo.getCustLevel());
		
		return "customer";
	}
	
	@RequestMapping("/customer/edit")
	@ResponseBody
	public Customer getCustomerById(Long id) {
		Customer customer = customerService.getCustomerById(id);
		return customer;
	}
	
	@RequestMapping(value="/customer/update", method=RequestMethod.POST)
	@ResponseBody
	public String updateCustomer(Customer customer) {
		customerService.updateCustomer(customer);
		return "OK";
	}
	
	@RequestMapping("/customer/delete")
	@ResponseBody
	public String deleteCustomer(Long id) {
		customerService.deleteCustomer(id);
		return "OK";
	}

Service层

	@Autowired
	private CustomerDao customerDao;
	@Override
	public Page<Customer> getCustomerList(QueryVo queryVo) {
		//计算分页起始记录
		if (queryVo.getPage() != null) {
			queryVo.setStart((queryVo.getPage() - 1) * queryVo.getSize());
		}
		List<Customer> customerList = customerDao.getCustomerList(queryVo);
		//创建一个Page对象
		Page<Customer> page = new Page<>();
		page.setRows(customerList);
		//查询总记录数
		Integer count = customerDao.getCustomerListCount(queryVo);
		page.setTotal(count);
		page.setSize(queryVo.getSize());
		page.setPage(queryVo.getPage());
		//返回结果
		return page;
	}
	@Override
	public Customer getCustomerById(Long id) {
		Customer customer = customerDao.getCustomerById(id);
		return customer;
	}
	@Override
	public void updateCustomer(Customer customer) {
		
		customerDao.updateCustomer(customer);
		
	}
	@Override
	public void deleteCustomer(Long id) {
		customerDao.deleteCustomer(id);
	}

dao层


public interface CustomerDao {
	List<Customer> getCustomerList(QueryVo queryVo);
	Integer getCustomerListCount(QueryVo queryVo);
	Customer getCustomerById(Long id);
	void updateCustomer(Customer customer);
	void deleteCustomer(Long id);
}
	<sql id="customer_list_where">
		<where>
			<if test="custName !=null and custName != ''">
				and cust_name LIKE '%${custName}%'
			</if>
			<if test="custSource !=null and custSource != ''">
				AND cust_source = #{custSource}
			</if>
			<if test="custIndustory !=null and custIndustory != ''">
				AND cust_industry = #{custIndustory}
			</if>
			<if test="custLevel !=null and custLevel != ''">
				AND cust_level = #{custLevel}
			</if>
		</where>
	</sql>
	<select id="getCustomerList" 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="customer_list_where"/>
		<if test="start != null">
			limit #{start},#{size}
		</if>
	</select>
	<select id="getCustomerListCount" parameterType="queryvo" resultType="int">
		SELECT count(0)
		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="customer_list_where"/>
	</select>
	<select id="getCustomerById" parameterType="long" resultType="customer">
		SELECT *
		FROM
			customer 
		where cust_id = #{id}
	</select>
	<update id="updateCustomer" parameterType="customer">
		update customer
		<set>
			<if test="cust_name!=null">
				cust_name=#{cust_name},
			</if>
			<if test="cust_user_id!=null">
				cust_user_id=#{cust_user_id},
			</if>
			<if test="cust_create_id!=null">
				cust_create_id=#{cust_create_id},
			</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>
			<if test="cust_createtime!=null">
				cust_createtime=#{cust_createtime},
			</if>
		</set>
		WHERE cust_id=#{cust_id}
	</update>
	<delete id="deleteCustomer" parameterType="long">
		DELETE
		FROM
			customer
		WHERE
			cust_id = #{id}
	</delete>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值