主要对上一版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框架版本的更多相关文章

  1. 简易的CRM系统案例之SpringMVC+JSP+MySQL+hibernate框架版本

    继续对上一版本进行改版,变成SpringMVC框架 简易的CRM系统案例之易的CRM系统案例之JSP+MySQL+SSH框架版本 src/spring.xml <?xml version=&qu ...

  2. 简易的CRM系统案例之Struts2+JSP+MySQL版本

    对简易的CRM系统案例之Servlet+Jsp+MySQL版本改进 Servlet优化为Struts2 学习 <?xml version="1.0" encoding=&qu ...

  3. 简易的CRM系统案例之Servlet+Jsp+MySQL版本

    数据库配置 datebase.properties driver=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/infos usernam ...

  4. 简易的CRM系统案例之Struts2+Hibernate3+JSP+MySQL版本

    改造上一版本的DAO层 简易的CRM系统案例之Struts2+JSP+MySQL版本 src文件下hibernate.cfg.xml <!DOCTYPE hibernate-configurat ...

  5. 简易的CRM系统案例之易的CRM系统案例之JSP+MySQL+SSH框架版本

    主要对上一篇hibernate与Spring进行整合改进 简易的CRM系统案例之Struts2+Hibernate3+JSP+MySQL版本 bean-base.xml <?xml versio ...

  6. 简易的CRM系统案例之Struts2&Spring整合+Hibernate3+JSP+MySQL版本

    主要对上一篇Struts2&Spring整合的改造 简易的CRM系统案例之Struts2+Hibernate3+JSP+MySQL版本 src/bean.xml <beans xmlns ...

  7. 简易的CRM系统案例SpringBoot + thymeleaf + MySQL + MyBatis版本

    创建maven项目 pop.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns ...

  8. 好看又能打的CRM系统大比拼:Salesforce, SugarCRM, Odoo等

    介绍 今天的CRM市场提供了大量的解决方案和软件替代品.有些适合大型企业(通常需要内部托管),而其他企业则更多地应用于SME的需求(通常使用云托管解决方案). 在CRM解决方案方面,提供商必须调整其产 ...

  9. CRM系统新思维

    客户关系管理系统(CRM系统)是管理公司当前以及未来潜在客户的系统,其主要目的是通过优化客户关系实现公司销售业绩的长期增长,它是企业信息系统的核心之一.目前,移动互联网.大数据以及人工智能技术发展日新 ...

随机推荐

  1. MySQL 数据库的高可用性分析

    MySQL数据库是目前开源应用最大的关系型数据库,有海量的应用将数据存储在MySQL数据库中.存储数据的安全性和可靠性是生产数据库的关注重点.本文分析了目前采用较多的保障MySQL可用性方案. MyS ...

  2. springboot ElasticSearch 简单的全文检索高亮

    原文:https://segmentfault.com/a/1190000017324038?utm_source=tag-newest 首先引入依赖 <dependency> <g ...

  3. CGI FastCGI php-FPM 分别是什么

    1.CGI协议用于php解析器跟webserver之间的通信(效率低,浪费资源) 2.FastCGI 可以一次性处理多个进程,是CGI的改良版本 3.php-FPM 是FastCGI 的进程管理器(产 ...

  4. man与info

    Linux系统中在线求助命令:man page 与info page 还有--help . --help没有man的详细,首先我们来看mna 命令.在linux中输入 man + 相关的文件 ,就可以 ...

  5. win32通用控件

    1.标准控件 可以在win32窗口程序中添加资源脚本来给程序添加标准控件: 具体操作为:新建资源脚本    ->在.rc文件中添加控件    ->给控件绑定事件:   常用的标准控件:   ...

  6. 【安卓基础】ViewPager2的入门使用

    之前的项目中使用过ViewPager,被坑过几次.如果你在RecyclerView中的Item使用ViewPager,你绝对会产生莫名其妙的问题,因为ViewPager在同一界面上不能有两个一样的ID ...

  7. PHP怎么实现字符串转义和还原?

    首先大家可以简单了解下什么是转义字符?有什么用? 转义字符是一种特殊的字符常量.转义字符以反斜线"\"开头,后跟一个或几个字符.转义字符具有特定的含义,不同于字符原有的意义,故称“ ...

  8. MongoDB 查看chunk块大小

    使用mongo shell连到mongos执行命令:AllChunkInfo("dbname.cellname",true) 点击(此处)折叠或打开 AllChunkInfo = ...

  9. SYSTEM_INFORMATION_CLASS

    source: https://github.com/processhacker/processhacker/blob/master/phnt/include/ntexapi.h // rev // ...

  10. DOM Composition 事件

    做实时的表单表单校验时,如果输入的是非拉丁语言,那你可能会遇到下面的问题: 如上图所示,文本框不允许输入 ' 之类的特殊字符,当用户在敲击拼音.还未最终输入时就已经触发了校验,提示输入不合法,有点尴尬 ...