简易的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
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!