applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 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-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!--读取db.properties --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <!--数据库驱动 --> <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}" /> <!--最大连接数 --> <property name="maxTotal" value="${jdbc.maxTotal}" /> <!--最大空闲连接 --> <property name="maxIdle" value="${jdbc.maxIdle}" /> <!--初始化连接数 --> <property name="initialSize" value="${jdbc.initialSize}" /> </bean> <!-- 事务管理器 --> <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.core.service.*.*(..))" /> </aop:config> <!-- 配置 MyBatis的工厂 --> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 数据源 --> <property name="dataSource" ref="dataSource" /> <!-- 配置MyBatis的核心配置文件所在位置 --> <property name="configLocation" value="classpath:mybatis-config.xml" /> </bean> <!-- 接口开发,扫描 com.core.dao包 ,写在此包下的接口即可被扫描到 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.core.dao" /> </bean> <!-- 配置扫描@Service注解 --> <context:component-scan base-package="com.core.service"/> </beans>
db.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/boot_crm jdbc.username=root jdbc.password=098879 jdbc.maxTotal=30 jdbc.maxIdle=10 jdbc.initialSize=5
log4j.properties
# Global logging configuration log4j.rootLogger=ERROR, stdout # MyBatis logging configuration... log4j.logger.com.core=DEBUG # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
mybatis-config.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> <!-- 别名定义 --> <typeAliases> <!-- 采用了直接自动扫描包的形式,这是在别名过多的情况下使用,mybatis会将所有的com.core.po包 中的pojo类以首字母小写的非限定类名作为它的别名。 --> <package name="com.core.po" /> </typeAliases> </configuration>
resource.properties
customer.from.type=002 customer.industry.type=001 customer.level.type=006
springmvc-config.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 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-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 加载属性文件,可以方便我们在一个阶段内不需要频繁写一个参数的值,而在不同阶段间又可以方便的切换参数的配置信息 --> <context:property-placeholder location="classpath:resource.properties" /> <!-- 配置扫描器 --> <context:component-scan base-package="com.core.web.controller" /> <!-- 注解驱动:配置处理器映射器和适配器 --> <mvc:annotation-driven /> <!--配置静态资源的访问映射,此配置中的文件,将不被前端控制器拦截 --> <mvc:resources location="/js/" mapping="/js/**" /> <mvc:resources location="/css/" mapping="/css/**" /> <mvc:resources location="/fonts/" mapping="/fonts/**" /> <mvc:resources location="/images/" mapping="/images/**" /> <!-- 配置视图解释器ViewResolver --> <bean id="jspViewResolver" class= "org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 配置拦截器 ,实现的第一个功能是进行登录拦截,在登录之前验证(preHandle)--> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**" /><!-- 拦截所有的请求 --> <bean class="com.core.interceptor.LoginInterceptor" /> </mvc:interceptor> </mvc:interceptors> </beans>
BaseDictDao.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" > <mapper namespace="com.core.dao.BaseDictDao"> <!-- 根据类别代码查询 --> <select id="selectBaseDicByTypeCode" parameterType="String" resultType="baseDict"> <!-- 这里写baseDict是因为在config中的mybatis-config中写了别名 --> select * from base_dict where dict_type_code=#{typecode} </select> </mapper>
CustomerDao.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" > <mapper namespace="com.core.dao.CustomerDao" > <!-- 对应于CustomerDao的xml文件 --> <!-- 建立一个sql片段 --> <sql id="selectCustomListWhere"> <where> <if test="cust_name!=null"> cust_name LIKE "%"#{cust_name}"%" </if> <if test="cust_source!=null"> AND cust_source =#{cust_source} </if> <if test="cust_industry !=null"> AND cust_industry=#{cust_industry} </if> <if test="cust_level!=null"> AND cust_level=#{cust_level} </if> </where> </sql> <select id="selectCustomList" parameterType="Customer" resultType="customer"> <!-- 这里写customer是因为在config中的mybatis-config中写了别名 --> select cust_id, cust_name, cust_user_id, cust_create_id, b.dict_item_name cust_source, c.dict_item_name cust_industry, d.dict_item_name cust_level, cust_linkman, cust_phone, cust_mobile, cust_createtime FROM customer a LEFT JOIN(select dict_id,dict_item_name from base_dict WHERE dict_type_code='002') b ON a.cust_source=b.dict_id LEFT JOIN(select dict_id,dict_item_name from base_dict WHERE dict_type_code='001') c ON a.cust_industry=c.dict_id LEFT JOIN(select dict_id,dict_item_name from base_dict WHERE dict_type_code='006') d ON a.cust_level=d.dict_id <include refid="selectCustomListWhere"></include> <!-- 这个是执行分页查询 --> <if test="start!=null and rows!=null"> LIMIT #{start},#{rows}; </if> </select> <select id="selectCustomListCount" parameterType="Customer" resultType="Integer"> select count(*) from customer <include refid="selectCustomListWhere"></include> </select> <!-- 添加客户 --> <insert id="createCustomer" parameterType="customer"><!-- 会返回一个影响行数 --> insert into customer( cust_name, cust_user_id, cust_create_id, cust_source, cust_industry, cust_level, cust_linkman, cust_phone, cust_mobile, cust_zipcode, cust_address, cust_createtime )values( #{cust_name}, #{cust_user_id}, #{cust_create_id}, #{cust_source}, #{cust_industry}, #{cust_level}, #{cust_linkman}, #{cust_phone}, #{cust_mobile}, #{cust_zipcode}, #{cust_address}, #{cust_createtime} ); </insert> <!-- 通过id查询Customer对象 --> <select id="getCustomerById" parameterType="Integer" resultType="customer"> select * from customer where cust_id=#{id} </select> <!-- 更新客户 --> <update id="updateCustomer" parameterType="customer"> update customer <set><!-- 通过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} </where> </update> <!-- 通过id进行删除 --> <delete id="deleteCustomer" parameterType="Integer"> delete from customer where cust_id=#{id} </delete> </mapper>
UserDao.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" > <mapper namespace="com.core.dao.UserDao" > <!-- 对应于UserDao的xml文件 --> <!-- 查询用户 --> <select id="findUser" parameterType="String" resultType="user"> <!-- 这里写user是因为在config中的mybatis-config中写了别名 --> select * from sys_user where user_code = #{usercode} and user_password =#{password} and user_state = '1' </select> </mapper>