MyBatis、SpringMvc 练习
CRM 系统
(客户关系管理系统)
1 数据库
数据库使用 mysql 数据库。
1、创建 crm 数据库
2、将参考资料中的 sql 脚本(crm.sql)导入到数据库中
3、注意:你需要好好的,仔细的分析每张表。(这是一个重难点)
2 工程搭建
工程使用 SpringMvc、Spring、Myb atis 框架整合完成。
2.1 SqlMapConfig.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>
</configuration>
2.2 applicationContext-dao.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"
xmlns:task="http://www.springframework.org/schema/task"xmlns:dubbo="http://
code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 配置 读取 properties 文件 db.properties -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置 数据源 -->
<beanid="dataSource"class="com.alibaba.druid.pool.DruidDataSource">
<!-- 驱动 -->
<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}"/>
</bean>
<!-- 配置 Mybatis
的工厂 sqlSession-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置 Mybatis
的核心配置文件所在位置 -->
<property name="configLocation"value="classpath:SqlMapConfig.xml"/>
<!-- 配置 pojo
别名 -->
<property name="typeAliasesPackage" value="cn.itcast.core.bean"></property>
</bean>
<!-- 配置 1:原始 Dao
开发 接口 接口实现类 Mapper.xml 三个
2:接口开发 接口 不写实现类 Mapper.xml 二个 (UserDao、ProductDao
、BrandDao。。。。。。。)
3:接口开发、并支持扫描 cn.itcast.core.dao(UserDao。。。。。) 写在此包下即可被扫描到 --
>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.itcast.core.dao"/>
</bean>
</beans>
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm_crm?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
2.3 applicationContext-service.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"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 配置 扫描 @Service -->
<context:component-scan base-package="cn.itcast.core.service"/>