个人笔记之CRM项目搭建

26 篇文章 0 订阅
25 篇文章 0 订阅

搭建一个用于练习的CRM项目

1.pom.xml文件的配置

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.cl</groupId>
  <artifactId>crm</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>crm</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.2.RELEASE</version>
  </parent>

  <dependencies>

    <!-- web 环境 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- aop -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

    <!-- freemarker -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
    <!-- 测试环境 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <!-- mybatis -->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.1.1</version>
    </dependency>

    <!-- 分⻚插件 -->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper-spring-boot-starter</artifactId>
      <version>1.2.13</version>
    </dependency>
    <!-- mysql -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>
    <!-- c3p0 -->
    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.5</version>
    </dependency>

    <!-- commons-lang3 -->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.5</version>
    </dependency>

    <!-- json -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.47</version>
    </dependency>
    <!-- DevTools 热部署 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <optional>true</optional>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.2</version>
        <configuration>
          <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
          <verbose>true</verbose>
          <overwrite>true</overwrite>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <!-- 如果没有该配置,热部署的devtools不⽣效 -->
          <fork>true</fork>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

2.application.yml文件的配置

## 端⼝号 上下⽂路径
server:
  port: 9090
  servlet:
    context-path: /crm01
## 数据源配置
spring:
  datasource:
    type: com.mchange.v2.c3p0.ComboPooledDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/crm?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
    username: root
    password: 1234
## freemarker,注意它与datasource是同级的
  freemarker:
    suffix: .ftl
    content-type: text/html
    charset: UTF-8
    template-loader-path: classpath:/views/
## 启⽤热部署
	devtools:
	  restart:
	    enabled: true
	    additional-paths: src/main/java
## 设置返回格式乱码
	  http:
	    encoding:
	      charset: utf-8
	      force-response: true

## mybatis 配置
mybatis:
  mapper-locations: classpath:/mappers/*.xml
  type-aliases-package: com.cl.crm.vo;com.cl.crm.query;com.cl.crm.dto
  configuration:
    map-underscore-to-camel-case: true
## pageHelper 分⻚
pagehelper:
  helper-dialect: mysql

## 设置 dao ⽇志打印级别
logging:
  level:
    com:
      cl:
        crm:
          dao: debug

#F:\m2\repository\mysql\mysql-connector-java\8.0.18\mysql-connector-java-8.0.18.jar

3.generatorConfig.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <!-- 数据库驱动路径:在左侧project边栏的External Libraries中找到mysql的驱动,右键选择copy
   path -->
    <classPathEntry
            location="F:\m2\repository\mysql\mysql-connector-java\8.0.18\mysql-connector-java-8.0.18.jar"/>
    <!-- context 是逆向⼯程的主要配置信息,id:起个名字,targetRuntime:设置⽣成的⽂件适⽤于哪个
   mybatis版本 -->
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!--optional,指在创建class时,对注释进⾏控制-->
        <commentGenerator>
            <!-- 是否去除⽇期那⾏注释 -->
            <property name="suppressDate" value="true"/>
            <!-- 是否去除⾃动⽣成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!-- 数据库链接地址账号密码 -->
        <jdbcConnection
                driverClass="com.mysql.cj.jdbc.Driver"
                connectionURL="jdbc:mysql://127.0.0.1:3306/crm?serverTimezone=GMT%2B8"
                userId="root"
                password="1234">
                            <!--Mysql如果不使用此属性,在生成时会报错-->
            <property name="nullCatalogMeansCurrent" value="true"/>
        </jdbcConnection>
        <!--
        java类型处理器
        ⽤于处理DB中的类型到Java中的类型,默认使⽤JavaTypeResolverDefaultImpl;
        注意⼀点,默认会先尝试使⽤Integer,Long,Short等来对应DECIMAL和NUMERIC数据类
       型;
        true:使⽤ BigDecimal对应DECIMAL和NUMERIC数据类型
        false:默认,把JDBC DECIMAL和NUMERIC类型解析为Integer
        -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- ⽣成Model类存放位置 -->
        <javaModelGenerator targetPackage="com.cl.crm.vo"
                            targetProject="src/main/java">
            <!-- 在targetPackage的基础上,根据数据库的schema再⽣成⼀层package,⽣成的类放在这个
           package下,默认为false -->
            <property name="enableSubPackages" value="true"/>
            <!-- 设置是否在getter⽅法中,对String类型字段调⽤trim()⽅法 -->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--⽣成映射⽂件存放位置-->
        <sqlMapGenerator targetPackage="mappers" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--⽣成Dao类存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.cl.crm.dao"
                             targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 数据库的表名与对应的实体类的名称,tableName是数据库中的表名,domainObjectName是⽣成
       的JAVA模型名 -->
        <table tableName="t_user" domainObjectName="User"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>

逆向生成启动指令:mybatis-generator:generate -e
至此,项目的基本设置就配置完成了,接下来只需要根据需求添加相应的包和类就好了。

crm项目的架构 * 创建web工程 * 引入jar包 * mysql的驱动包 * hibernate需要的jar包 * spring需要的jar包 * struts2需要的jar包 * jstl 需要的jar包 * junit需要的jar包 * 包的介绍 * cn.itcast.crm.container:重新封装spring容器 * cn.itcast.crm.dao:放置的是dao接口 * cn.itcast.crm.dao.impl:放置的是dao接口的实现类 * cn.itcast.crm.domain:放置的是po类、po类和数据库表关联的映射文件 * cn.itcast.crm.service:业务层的接口 * cn.itcast.crm.service.impl:业务层的接口的实现类 * cn.itcast.crm.web.action:struts2的action * cn.itcast.crm.web.form:封装页面数据的javaBean * junit:开发人员测试用的 * 搭建hibernate层 * 定义需求:部门信息的页面数据要插入到数据库中 * 定义表 CREATE TABLE `sys_user_group` ( `id` INTEGER(11) NOT NULL AUTO_INCREMENT, #编号 `remark` TEXT, #备注 `name` VARCHAR(100) DEFAULT NULL, #部门名称 `principal` VARCHAR(50) DEFAULT NULL, #部门负责人 `incumbent` VARCHAR(200) DEFAULT NULL, #部门职能 PRIMARY KEY (`id`) ) * 创建po对象,放置在cn.itcast.crm.domain包下 public class SysUserGroup implements java.io.Serializable { private Integer id; private String remark; // 备注 private String name; // 部门名称 private String principal; // 部门负责人 private String incumbent; // 部门职能 } * 创建表和po之间的映射文件SysUserGroup.hbm.xml 放置在cn.itcast.crm.domain包下 * SysUserGroup.hbm.xml文件的内容直接看该文件即可 * 创建hibernate.cfg.xml文件连接数据库,加载SysUserGroup.hbm.xml文件,放置src下 <session-factory> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/itcast0807crm</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.autocommit">true</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.show_sql">true</property> <mapping resource="cn/itcast/crm/domain/SysUserGroup.hbm.xml"/> </session-factory> * 测试.放置在junit包下 public class TestHibernate { @Test public void testHibernateConf(){ Configuration config=new Configuration(); config.configure(); SessionFactory sf=config.buildSessionFactory(); Session s=sf.openSession(); Transaction tx=s.beginTransaction(); SysUserGroup sysUserGroup=new SysUserGroup(); sysUserGroup.setName("销售部"); sysUserGroup.setPrincipal("xxx"); sysUserGroup.setIncumbent("ttt"); s.save(sysUserGroup); tx.commit(); s.close(); } } * 搭建spring和hibernate * 创建beans.xml文件,放置在src下 * 引入命名空间 bean tx context aop <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> * 配置如下: <!-- 1 配置注解的自动扫描--> <context:component-scan base-package="cn.itcast.crm"/> <!--3 配置本地化代理工程bean,这是spring整合hibernate的入口 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <!--表示在类路径下加载hibernate.cfg.xml --> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <!--4 创建事务管理器 aop切面--> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!--5 配置处理事务的注解--> <tx:annotation-driven transaction-manager="txManager"/> * 创建dao层共同的接口,放置在cn.itcast.crm.dao下 public interface ICommonDao<T> { public void save(T entity); } * 创建dao层共同的接口的实现类,,放置在cn.itcast.crm.dao.impl下 public class CommonDaoImpl<T> extends HibernateDaoSupport implements ICommonDao<T> { public void save(T entity) { this.getHibernateTemplate().save(entity); } @Resource(name="sessionFactory") //注入sessionFactory public void setSessionFactoryDI(SessionFactory sessionFactory) { System.out.println("sessionFactory "+sessionFactory); //调用父类的setSessionFactory方法,注入sessionFactory super.setSessionFactory(sessionFactory); } } * 创建部门的dao接口 SysUserGroupDao接口 放在cn.itcast.crm.dao包下 public interface ISysUserGroupDao extends ICommonDao<SysUserGroup> { public final static String SERVICE_NAME="cn.itcast.crm.dao.impl.SysUserGroupDaoImpl"; } * 创建部门的dao接口的实现类 放在cn.itcast.crm.dao.impl包下 @Repository(ISysUserGroupDao.SERVICE_NAME) public class SysUserGroupDaoImpl extends CommonDaoImpl<SysUserGroup> implements ISysUserGroupDao { } * 测试,放置在junit包下 public class TestSysUserGroupDao { @Test public void testSave() { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); ISysUserGroupDao sysUserGroupDao = (ISysUserGroupDao) ctx.getBean(ISysUserGroupDao.SERVICE_NAME); SysUserGroup sysUserGroup = new SysUserGroup(); sysUserGroup.setName("销售部"); sysUserGroup.setPrincipal("xxx"); sysUserGroup.setIncumbent("ttt"); sysUserGroupDao.save(sysUserGroup); } } * 创建部门的业务层接口 ,放置在cn.itcast.crm.service包下 public interface ISysUserGroupService { public final static String SERVICE_NAME="cn.itcast.crm.service.impl.SysUserGroupServiceImpl"; public void saveSysUserGroup(SysUserGroup sysUserGroup); } * 创建部门的业务层接口实现类,放置在cn.itcast.crm.service.impl包下 @Transactional(readOnly=true) @Service(ISysUserGroupService.SERVICE_NAME) public class SysUserGroupServiceImpl implements ISysUserGroupService { @Resource(name=ISysUserGroupDao.SERVICE_NAME) private ISysUserGroupDao sysUserGroupDao; @Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false) public void saveSysUserGroup(SysUserGroup sysUserGroup) { sysUserGroupDao.save(sysUserGroup); } } * 测试 public class TestSysUserGroupService { @Test public void testSavex() { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); ISysUserGroupService sysUserGroupService = (ISysUserGroupService) ctx.getBean(ISysUserGroupService.SERVICE_NAME); SysUserGroup sysUserGroup = new SysUserGroup(); sysUserGroup.setName("销售部"); sysUserGroup.setPrincipal("xxx"); sysUserGroup.setIncumbent("ttt"); sysUserGroupService.saveSysUserGroup(sysUserGroup); } } * 搭建struts2(保存部门信息) * jsp页面(sys/group/add.jsp) * 请求的路径/sys/sysUserGroupAction_save.do * 根据路径创建SysUserGroupAction和在action中增加save方法,放置在cn.itcast.crm.web.action包下 public class SysUserGroupAction extends ActionSupport public String save() throws IllegalAccessException, InvocationTargetException{ System.out.println("xxxxxxxxxxxxxxxxxxxxxxxx "); return null; } } * 建立请求路径和action之间的关联 * 创建struts.xml文件,放置在src下 * 文件内容如下: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <!-- 配置请求后缀名.do --> <constant name="struts.action.extension" value="do"/> <!-- 配置主题为简单主题 --> <constant name="struts.ui.theme" value="simple"/> <!--配置struts2的模式为开发模式--> <constant name="struts.devMode" value="true"/> <package name="sys" namespace="/sys" extends="struts-default"> <action name="sysUserGroupAction_*" class="cn.itcast.crm.web.action.SysUserGroupAction" method="{1}"> <result name="add">/sys/group/add.jsp</result> </action> </package> </struts> * 在web.xml文件配置struts2的过滤器 <filter> <filter-name>StrutsPrepareAndExecuteFilter</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>StrutsPrepareAndExecuteFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> * 测试 * 定义javaBean(vo对象)接收表单数据,放置在cn.itcast.crm.web.form包下 public class SysUserGroupForm implements java.io.Serializable { private String id; private String name; private String principal; private String incumbent; private String remark; } * action要实现模型驱动接口 public class SysUserGroupAction extends ActionSupport implements ModelDriven<SysUserGroupForm>{ private SysUserGroupForm sysUserGroupForm=new SysUserGroupForm(); public String save() throws IllegalAccessException, InvocationTargetException{ System.out.println("xxxxxxxxxxxxxxxxxxxxxxxx "); return null; } public SysUserGroupForm getModel() { return sysUserGroupForm; } } * 测试,SysUserGroupForm是否接收到值 * 如何在struts2的SysUserGroupAction中的save方法中,获取spring容器中bean节点的对象 * 创建ServiceProvinderCore,加载beans.xml文件,放置在cn.itcast.crm.container包下 public class ServiceProvinderCore { protected ApplicationContext ctx; /** * @param filename beans.xml */ public void load(String filename){ ctx=new ClassPathXmlApplicationContext(filename); } } * 创建ServiceProvinder类,获取获取spring容器中bean节点的对象,放置在cn.itcast.crm.container包下 public class ServiceProvinder { private static ServiceProvinderCore sc; static{ System.err.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); sc=new ServiceProvinderCore(); sc.load("beans.xml"); } public static Object getService(String beanName){ System.err.println("ppppppppppppppppppppppp"); if(StringUtils.isBlank(beanName)){ throw new RuntimeException("您要访问的服务名称不能为空"); } Object bean=null; //如果spring容器中包含beanName if(sc.ctx.containsBean(beanName)){ bean=sc.ctx.getBean(beanName); } //如果spring容器中不包含beanName if(bean==null){ throw new RuntimeException("您要访问的服务名称["+beanName+"]不存在"); } return bean; } } * 测试 @Test public void testSave() { ISysUserGroupService sysUserGroupService=(ISysUserGroupService)ServiceProvinder.getService(ISysUserGroupService.SERVICE_NAME); SysUserGroup sysUserGroup = new SysUserGroup(); sysUserGroup.setName("销售部"); sysUserGroup.setPrincipal("xxx"); sysUserGroup.setIncumbent("ttt"); sysUserGroupService.saveSysUserGroup(sysUserGroup); } * 在 SysUserGroupAction中的save方法中增加如下代码 public String save() throws IllegalAccessException, InvocationTargetException{ System.out.println("sysUserGroupForm.getName() "+sysUserGroupForm.getName()); //实例化po对象 SysUserGroup sysUserGroup=new SysUserGroup(); //赋值vo对象的值到po中 BeanUtils.copyProperties(sysUserGroup, sysUserGroupForm); //获取业务层的对象(本项目struts2和spring是分离的) ISysUserGroupService sysUserGroupService= (ISysUserGroupService)ServiceProvinder.getService(ISysUserGroupService.SERVICE_NAME); //调用业务层保存po对象 sysUserGroupService.saveSysUserGroup(sysUserGroup); return null; } * 测试:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值