简易的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系统)是管理公司当前以及未来潜在客户的系统,其主要目的是通过优化客户关系实现公司销售业绩的长期增长,它是企业信息系统的核心之一.目前,移动互联网.大数据以及人工智能技术发展日新 ...
随机推荐
- git 添加码云远程仓库和上传到码云的命令
添加远程仓库 git remote add Zk 仓库地址.git 查看远程仓库 git remote -v 上传远程仓库 git push Zk master 删除远程仓库Zkgit remot ...
- 1260:【例9.4】拦截导弹(Noip1999)
题目来源:http://ybt.ssoier.cn:8088/problem_show.php?pid=1260 1260:[例9.4]拦截导弹(Noip1999) 时间限制: 1000 ms ...
- MySQL 表查询
表查询 前期准备一张表 create table emp( id int not null unique auto_increment, name varchar(32) not null, gend ...
- PHP 判断给定两个时间是否在同一周,月,年
判断是否在同一周 date_default_timezone_set('PRC'); //判断是否在同一周,原理:求出其中一个时间戳所在周的周一凌晨时间戳和周日24.00时间戳,如果另一个时间戳在这个 ...
- SpringMVC使用ResponseEntity实现文件下载,及图片base64的字节数组上传于下载
本文主要通过ResponseEntity<byte[]>实现文件下 该类实现响应头.文件数据(以字节存储).状态封装在一起交给浏览器处理以实现浏览器的文件下载. ResponseEntit ...
- GreenPlum 数据库创建用户、文件空间、表空间、数据库
前几篇文章介绍了GreenPlum数据库的安装.启动.关闭.状态检查.登录等操作,数据库已经创建好了,接下来介绍如何使用数据库.按照习惯,需要先创建测试用户.表空间.数据库.先创建测试用户dbdrea ...
- span标签
<span>在行内定义一个区域,也就是一行内可以被<span>划分成好几个区域,从而实现某种特定效果.<span>本身没有任何属性. <span> 与& ...
- xhEditor编辑器从word粘贴公式
我司需要做一个需求,就是使用富文本编辑器时,不要以上传附件的形式上传图片,而是以复制粘贴的形式上传图片. 在网上找了一下,有一个插件支持这个功能. WordPaster 安装方式如下: 直接使用Wor ...
- package.json文件说明解释
1.package.json是什么? 什么是Node.js的模块(Module)?在Node.js中,模块是一个库或框架,也是一个Node.js项目.Node.js项目遵循模块化的架构,当我们创建了一 ...
- 国庆集训 Day1 T2 生成图 DP
国庆集训 Day1 T2 生成图 现在要生成一张\(n\)个点的有向图.要求满足: 1.若有 a->b的边,则有 b->a 的边 2.若有 a->b 的边和 b->c 的边,则 ...