简易的CRM系统案例之SpringMVC+JSP+MySQL+myBatis框架版本
主要对上一版DAO框架的替换hibernate变成myBatis
简易的CRM系统案例之SpringMVC+JSP+MySQL+hibernate框架版本
src/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> <!-- 加载映射文件-->
<mappers>
<mapper resource="com/loaderman/crm/entity/userMapper.xml"/>
<mapper resource="com/loaderman/crm/entity//accountMapper.xml"/>
<mapper resource="com/loaderman/crm/entity//policyMapper.xml"/>
</mappers> </configuration>
accountMapper.xml
<?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"> <!-- namespace属性是名称空间,必须唯一 -->
<mapper namespace="com.loaderman.crm.entity.Account"> <select id="findIsHas" parameterType="com.loaderman.crm.entity.Account" resultType="com.loaderman.crm.entity.Account">
select * from t_account where username=#{username} and password=#{password}
</select> </mapper>
userMapper.xml
<?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"> <!-- namespace属性是名称空间,必须唯一 -->
<mapper namespace="com.loaderman.crm.entity.User"> <update id="modifyUser" parameterType="com.loaderman.crm.entity.User">
update t_user set id=#{id},name=#{name},tel=#{tel},age=#{age},telephone=#{telephone},idCard=#{idCard},address=#{address},weixin=#{weixin},qq=#{qq},email=#{email},job=#{job},area=#{area},grade=#{grade},remark=#{remark} where id=#{id} </update> <delete id="delUser" parameterType="com.loaderman.crm.entity.User">
delete from t_user where id = #{id}
</delete>
<insert id="addUser" parameterType="com.loaderman.crm.entity.User" >
insert into t_user (id,name,sex,age,telephone,idCard,address,weixin,qq,email,job,area,grade,remark) values(#{id},#{name},#{sex},#{age},#{telephone},#{idCard},#{address},#{weixin},#{qq},#{email},#{job},#{area},#{grade},#{remark})
</insert>
<select id="getUserByName" parameterType="String" resultType="com.loaderman.crm.entity.User">
select * from t_user where name=#{name}
</select >
<select id="getUserMoreInfo" parameterType="com.loaderman.crm.entity.User" resultType="com.loaderman.crm.entity.User">
select * from t_user where id=#{id}
</select>
<select id="findUser" parameterType="com.loaderman.crm.entity.User" resultType="com.loaderman.crm.entity.User">
select * from t_user where id=#{id}
</select>
<select id="getAllUser" resultType="com.loaderman.crm.entity.User">
select * from t_user
</select >
</mapper>
policyMapper.xml
<?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"> <!-- namespace属性是名称空间,必须唯一 -->
<mapper namespace="com.loaderman.crm.entity.Policy">
<update id="modifyPolicy" parameterType="com.loaderman.crm.entity.Policy">
update t_policy set id=#{id},name=#{name},tel=#{tel},policyID=#{policyID},policyName=#{policyName},orderTime=#{orderTime},orderType=#{orderType},policyMoney=#{policyMoney},insuranceCoverage=#{insuranceCoverage},orderPayYears=#{orderPayYears},safeguardYear=#{safeguardYear},profitPercentage=#{profitPercentage},profitMoney=#{profitMoney},remark=#{remark} where id=#{id} </update>
<delete id="delPolicy" parameterType="com.loaderman.crm.entity.Policy">
delete from t_policy where id = #{id}
</delete>
<insert id="addPolicy" parameterType="com.loaderman.crm.entity.Policy" >
insert into t_policy (id,name,tel,policyID,policyName,orderTime,orderType,policyMoney,insuranceCoverage,orderPayYears,safeguardYear,profitPercentage,profitMoney,remark) values(#{id},#{name},#{tel},#{policyID},#{policyName},#{orderTime},#{orderType},#{policyMoney},#{insuranceCoverage},#{orderPayYears},#{safeguardYear},#{profitPercentage},#{profitMoney},#{remark})
</insert>
<select id="getPolicyMoreInfoByName" parameterType="String" resultType="com.loaderman.crm.entity.Policy">
select * from t_policy where name=#{name}
</select >
<select id="getPolicyMoreInfo" parameterType="com.loaderman.crm.entity.Policy" resultType="com.loaderman.crm.entity.Policy">
select * from t_policy where id=#{id}
</select>
<select id="findPolicy" parameterType="com.loaderman.crm.entity.Policy" resultType="com.loaderman.crm.entity.Policy">
select * from t_policy where id=#{id}
</select> <select id="getAllPolicy" resultType="com.loaderman.crm.entity.Policy">
select * from t_policy
</select > </mapper>
package com.loaderman.crm.util; //当没有使用spring的时候可以使用 import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; /**
* 工具类
*/
public class MybatisUtil {
private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();
private static SqlSessionFactory sqlSessionFactory;
/**
* 加载位于src/mybatis.xml配置文件
*/
static{
try {
Reader reader = Resources.getResourceAsReader("mybatis.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 禁止外界通过new方法创建
*/
private MybatisUtil(){}
/**
* 获取SqlSession
*/
public static SqlSession getSqlSession(){
//从当前线程中获取SqlSession对象
SqlSession sqlSession = threadLocal.get();
//如果SqlSession对象为空
if(sqlSession == null){
//在SqlSessionFactory非空的情况下,获取SqlSession对象
sqlSession = sqlSessionFactory.openSession();
//将SqlSession对象与当前线程绑定在一起
threadLocal.set(sqlSession);
}
//返回SqlSession对象
return sqlSession;
}
/**
* 关闭SqlSession与当前线程分开
*/
public static void closeSqlSession(){
//从当前线程中获取SqlSession对象
SqlSession sqlSession = threadLocal.get();
//如果SqlSession对象非空
if(sqlSession != null){
//关闭SqlSession对象
sqlSession.close();
//分开当前线程与SqlSession对象的关系,目的是让GC尽早回收
threadLocal.remove();
}
} /**
* 测试
*/
public static void main(String[] args) {
Connection conn = MybatisUtil.getSqlSession().getConnection();
System.out.println(conn!=null?"连接成功":"连接失败");
}
}
bean-dao.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: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.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">
<bean id="accountDao" class="com.loaderman.crm.dao.impl.AccountDaoimp" scope="singleton" lazy-init="false">
<property name="sqlSessionFactory" ref="sqlSessionFactoryBeanID"/>
</bean>
<bean id="policyDao" class="com.loaderman.crm.dao.impl.PolicyDaoimp" scope="singleton" lazy-init="false">
<property name="sqlSessionFactory" ref="sqlSessionFactoryBeanID"/>
</bean>
<bean id="userDao" class="com.loaderman.crm.dao.impl.UserDaoimp" scope="singleton" lazy-init="false">
<property name="sqlSessionFactory" ref="sqlSessionFactoryBeanID"/>
</bean>
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
bean-base.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: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.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"> <!-- 配置C3P0连接池(即管理数据库连接) -->
<bean id="comboPooledDataSourceID" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/infos"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean> <!-- 配置SqlSessionFactoryBean(即替代MyBatisUtil工具类的作用) -->
<bean id="sqlSessionFactoryBeanID" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="comboPooledDataSourceID"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean> <!-- 配置事务管理器(即使用JDBC事务管理器) -->
<bean id="dataSourceTransactionManagerID" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="comboPooledDataSourceID"/>
</bean> <!-- 配置事务通知(即哪些方法需要事务) -->
<tx:advice id="tx" transaction-manager="dataSourceTransactionManagerID">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice> <!-- 配置事务切面(即哪些包中的类需要事务通知) -->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.loaderman.crm.dao.*.*(..))"/>
<aop:advisor advice-ref="tx" pointcut-ref="pointcut" />
</aop:config> </beans>
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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<import resource="config/bean-base.xml"/>
<!--<import resource="config/bean-action.xml"/>-->
<import resource="config/bean-dao.xml"/>
<!--<import resource="config/bean-service.xml"/>-->
<import resource="config/bean-entity.xml"/> <!-- Action控制器 -->
<context:component-scan base-package="com.loaderman.crm"/> <!-- 基于注解的映射器(可选) -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <!-- 基于注解的适配器(可选) -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!-- 视图解析器(可选) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/> </beans>
package com.loaderman.crm.dao.impl; import com.loaderman.crm.dao.BaseDao;
import com.loaderman.crm.dao.UserDao;
import com.loaderman.crm.entity.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Repository; import java.util.List; @Repository
public class UserDaoimp extends BaseDao implements UserDao {
// Spring与Hibernate整合: IOC容器注入
private SqlSessionFactory sqlSessionFactory; public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
} @Override
public List<User> getAllUser() {
SqlSession sqlSession = sqlSessionFactory.openSession();
List<User> list = sqlSession.selectList("com.loaderman.crm.entity.User.getAllUser");
sqlSession.commit();
return list;
} @Override
public User getUserMoreInfo(User user) {
SqlSession sqlSession = sqlSessionFactory.openSession();
User user1 = sqlSession.selectOne("com.loaderman.crm.entity.User.getUserMoreInfo",user);
sqlSession.commit();
return user1; } @Override
public List<User> getUserByName(String name) {
SqlSession sqlSession = sqlSessionFactory.openSession();
System.out.println("name"+name);
List<User> list = sqlSession.selectList("com.loaderman.crm.entity.User.getUserByName",name);
sqlSession.commit();
return list; } @Override
//添加学生
public int addUser(User user) {
SqlSession sqlSession = sqlSessionFactory.openSession();
int insert = sqlSession.insert("com.loaderman.crm.entity.User.addUser",user);
sqlSession.commit();
return insert; } @Override
//删除
public int delUser(User user) { SqlSession sqlSession = sqlSessionFactory.openSession();
int delete = sqlSession.delete("com.loaderman.crm.entity.User.delUser",user);
sqlSession.commit(); return delete;
} @Override
public int modifyUser(User user) {
SqlSession sqlSession = sqlSessionFactory.openSession();
int update = sqlSession.update("com.loaderman.crm.entity.User.modifyUser",user);
sqlSession.commit(); return update; } //查找指定的客户存在不存在
public boolean findUser(User user) {
SqlSession sqlSession = sqlSessionFactory.openSession();
User uer1 = sqlSession.selectOne("com.loaderman.crm.entity.User.findUser",user);
sqlSession.commit();
if (uer1!=null){
return true;
}else {
return false;
}
}
}
点击源码下载
数据库表:https://www.cnblogs.com/loaderman/p/10315968.html
简易的CRM系统案例之SpringMVC+JSP+MySQL+myBatis框架版本的更多相关文章
- 简易的CRM系统案例之SpringMVC+JSP+MySQL+hibernate框架版本
继续对上一版本进行改版,变成SpringMVC框架 简易的CRM系统案例之易的CRM系统案例之JSP+MySQL+SSH框架版本 src/spring.xml <?xml version=&qu ...
- 简易的CRM系统案例之Struts2+JSP+MySQL版本
对简易的CRM系统案例之Servlet+Jsp+MySQL版本改进 Servlet优化为Struts2 学习 <?xml version="1.0" encoding=&qu ...
- 简易的CRM系统案例之Servlet+Jsp+MySQL版本
数据库配置 datebase.properties driver=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/infos usernam ...
- 简易的CRM系统案例之Struts2+Hibernate3+JSP+MySQL版本
改造上一版本的DAO层 简易的CRM系统案例之Struts2+JSP+MySQL版本 src文件下hibernate.cfg.xml <!DOCTYPE hibernate-configurat ...
- 简易的CRM系统案例之易的CRM系统案例之JSP+MySQL+SSH框架版本
主要对上一篇hibernate与Spring进行整合改进 简易的CRM系统案例之Struts2+Hibernate3+JSP+MySQL版本 bean-base.xml <?xml versio ...
- 简易的CRM系统案例之Struts2&Spring整合+Hibernate3+JSP+MySQL版本
主要对上一篇Struts2&Spring整合的改造 简易的CRM系统案例之Struts2+Hibernate3+JSP+MySQL版本 src/bean.xml <beans xmlns ...
- 简易的CRM系统案例SpringBoot + thymeleaf + MySQL + MyBatis版本
创建maven项目 pop.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns ...
- 好看又能打的CRM系统大比拼:Salesforce, SugarCRM, Odoo等
介绍 今天的CRM市场提供了大量的解决方案和软件替代品.有些适合大型企业(通常需要内部托管),而其他企业则更多地应用于SME的需求(通常使用云托管解决方案). 在CRM解决方案方面,提供商必须调整其产 ...
- CRM系统新思维
客户关系管理系统(CRM系统)是管理公司当前以及未来潜在客户的系统,其主要目的是通过优化客户关系实现公司销售业绩的长期增长,它是企业信息系统的核心之一.目前,移动互联网.大数据以及人工智能技术发展日新 ...
随机推荐
- Python字典取键、值对
1. 取键:keys()方法 #spyder bb={'人才/可怕':23,'伏地魔&波特':'army','哈哈哈,人才,回合':'hhh'} for ii in bb.keys(): pr ...
- JS函数篇【2】
什么是函数 函数的作用,可以写一次代码,然后反复地重用这个代码. <h3 onload="add2(1,2,3);add3(4,5,6)"></h3> &l ...
- 用js刷剑指offer(二叉搜索树的后序遍历序列)
题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 牛客网链接 js代码 function Verif ...
- mniGraffle常用快捷键
OmniGraffle 是 Mac 上的绘图利器.Graffle 在很多方面对标 Windows 系统上的 Microsoft Visio,是制作各种文档的绝妙工具. 变换移动 放大:Cmd+Shif ...
- linux网络编程之socket编程(十二)
今天继续学习socket编程,期待的APEC会议终于在京召开了,听说昨晚鸟巢那灯火通明,遍地礼花,有点08年奥运会的架势,有种冲动想去瞅见一下习大大的真容,"伟大的祖国,我爱你~~~&quo ...
- 19 Jquery 属性
从 jQuery 1.6 开始, .prop()方法 方法返回 property 的值,而 .attr() 方法返回 attributes 的值. 例如, selectedIndex, tagName ...
- comm shell command
1.awk command 1.1 Purpose 1: want to distinct and then count and sort by num 1.1.1 Command: cat resu ...
- 函数指针,使用qsort,进行结构体排序
#include <stdio.h> #include <stdlib.h> #define STU_NAME_LEN 16 /*学生信息*/ typedef struct s ...
- Linux(centos 7) 安装nginx
在安装nginx之前需要安装依赖的包 一. gcc 安装安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装: yum install gcc- ...
- hive 的 beeline用法
先开启服务端: nohup hive --service metastore & nohup hive --service hiveserver2 & 进入beeline: beel ...