码迷,mamicode.com
首页 > 编程语言 > 详细

springmvc-crm-ssm整合

时间:2017-06-11 20:10:02      阅读:499      评论:0      收藏:0      [点我收藏+]

标签:dbcc   -o   rate   enable   asi   callback   oid   lock   package   

一、crm项目环境创建


1、导jar包,springmvc、jstl、分页、mybatis、文件上传、数据库驱动、spring、jackson、jersey

2、导入页面,jsp放在WEB-INF里面,css、js、image放在WebContent路径下



二、mybatis逆向工程

逆向工程就是让程序自己根据表结构创建javabean、mapper接口、mapper.xml配置文件(sql语句)
两种方法:

1、插件逆向工程:

①. 把plugins中的所有jar拷贝到eclipse的plugins文件夹之中, 把features中的所有文件夹拷贝到eclipse的features文件夹之中,重启eclipse,ok
               ②、导入配置文件generatorConfig-base.xml,对配置文件进行整理

需要修改的地方:
技术分享
技术分享 
技术分享 
技术分享 

技术分享

技术分享 


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration
  3. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5. <generatorConfiguration>
  6. <!-- classPathEntry:数据库的JDBC驱动的jar包地址 -->
  7. <classPathEntry
  8. location="G:\mysql-connector-java-5.1.7-bin.jar" />
  9. <context id="caigouTables" targetRuntime="MyBatis3">
  10. <commentGenerator>
  11. <!-- 是否去除自动生成的注释 true:是 : false:否 -->
  12. <property name="suppressAllComments" value="false" />
  13. </commentGenerator>
  14. <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
  15. <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
  16. connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg"
  17. userId="yycg"
  18. password="yycg">
  19. </jdbcConnection> -->
  20. <jdbcConnection driverClass="com.mysql.jdbc.Driver"
  21. connectionURL="jdbc:mysql://localhost:3306/crm"
  22. userId="root"
  23. password="123">
  24. </jdbcConnection>
  25. <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和
  26. NUMERIC 类型解析为java.math.BigDecimal -->
  27. <javaTypeResolver>
  28. <property name="forceBigDecimals" value="false" />
  29. </javaTypeResolver>
  30. <!-- targetProject:生成PO类的位置 -->
  31. <javaModelGenerator targetPackage="com.heima.domain"
  32. targetProject="crm_ssm">
  33. <!-- enableSubPackages:是否让schema作为包的后缀 -->
  34. <property name="enableSubPackages" value="true" />
  35. <!-- 从数据库返回的值被清理前后的空格 -->
  36. <property name="trimStrings" value="true" />
  37. </javaModelGenerator>
  38. <!-- targetProject:自动mapper的位置 -->
  39. <sqlMapGenerator targetPackage="com.heima.mapper"
  40. targetProject="crm_ssm">
  41. <property name="enableSubPackages" value="false" />
  42. </sqlMapGenerator>
  43. <javaClientGenerator type="XMLMAPPER"
  44. targetPackage="com.heima.mapper" implementationPackage="com.heima.mapper"
  45. targetProject="crm_ssm">
  46. <property name="enableSubPackages" value="false" />
  47. </javaClientGenerator>
  48. <table tableName="base_dict" ></table>
  49. <table tableName="customer" ></table>
  50. <!-- <table schema="" tableName="userjd" />
  51. <table schema="" tableName="usergys" />
  52. <table schema="" tableName="dictinfo" />
  53. <table schema="" tableName="dicttype" />
  54. <table schema="" tableName="basicinfo" /> -->
  55. </context>
  56. </generatorConfiguration>



操作:
技术分享

结果:技术分享
 
   

2、代码逆向工程

 ①、需要的jar包:技术分享
 ②、导入逆向工程配置文件:generatorConfig-base.xml
按照之前插件逆向工程那样的修改,但是targetProject应该都等于".\src"

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration
  3. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5. <generatorConfiguration>
  6. <!-- classPathEntry:数据库的JDBC驱动的jar包地址 -->
  7. <classPathEntry
  8. location="G:\mysql-connector-java-5.1.7-bin.jar" />
  9. <context id="caigouTables" targetRuntime="MyBatis3">
  10. <commentGenerator>
  11. <!-- 是否去除自动生成的注释 true:是 : false:否 -->
  12. <property name="suppressAllComments" value="false" />
  13. </commentGenerator>
  14. <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
  15. <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
  16. connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg"
  17. userId="yycg"
  18. password="yycg">
  19. </jdbcConnection> -->
  20. <jdbcConnection driverClass="com.mysql.jdbc.Driver"
  21. connectionURL="jdbc:mysql://localhost:3306/crm"
  22. userId="root"
  23. password="123">
  24. </jdbcConnection>
  25. <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和
  26. NUMERIC 类型解析为java.math.BigDecimal -->
  27. <javaTypeResolver>
  28. <property name="forceBigDecimals" value="false" />
  29. </javaTypeResolver>
  30. <!-- targetProject:生成PO类的位置 -->
  31. <javaModelGenerator targetPackage="com.heima.domain"
  32. targetProject=".\src">
  33. <!-- enableSubPackages:是否让schema作为包的后缀 -->
  34. <property name="enableSubPackages" value="true" />
  35. <!-- 从数据库返回的值被清理前后的空格 -->
  36. <property name="trimStrings" value="true" />
  37. </javaModelGenerator>
  38. <!-- targetProject:自动mapper的位置 -->
  39. <sqlMapGenerator targetPackage="com.heima.mapper"
  40. targetProject=".\src">
  41. <property name="enableSubPackages" value="false" />
  42. </sqlMapGenerator>
  43. <javaClientGenerator type="XMLMAPPER"
  44. targetPackage="com.heima.mapper" implementationPackage="com.heima.mapper"
  45. targetProject=".\src">
  46. <property name="enableSubPackages" value="false" />
  47. </javaClientGenerator>
  48. <table tableName="base_dict" ></table>
  49. <table tableName="customer" ></table>
  50. <!-- <table schema="" tableName="userjd" />
  51. <table schema="" tableName="usergys" />
  52. <table schema="" tableName="dictinfo" />
  53. <table schema="" tableName="dicttype" />
  54. <table schema="" tableName="basicinfo" /> -->
  55. </context>
  56. </generatorConfiguration>

  1. import java.io.File;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.mybatis.generator.api.MyBatisGenerator;
  5. import org.mybatis.generator.config.Configuration;
  6. import org.mybatis.generator.config.xml.ConfigurationParser;
  7. import org.mybatis.generator.internal.DefaultShellCallback;
  8. public class SqlSGen {
  9. public static void main(String[] args) throws Exception {
  10. SqlSGen();
  11. }
  12. private static void SqlSGen() throws Exception {
  13. List<String> warnings = new ArrayList<String>();
  14. boolean overwrite = true;
  15. File configFile = new File("src/generatorConfig-base.xml");
  16. ConfigurationParser cp = new ConfigurationParser(warnings);
  17. Configuration config = cp.parseConfiguration(configFile);
  18. DefaultShellCallback callback = new DefaultShellCallback(overwrite);
  19. MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
  20. callback, warnings);
  21. myBatisGenerator.generate(null);
  22. }
  23. }



三、项目知识点

1、用注解引入properties文件到java代码中

        ①、要在springmvc.xml中配置如下
         <context:property-placeholder location="classpath:resource.properties" />
        ②、注解:@Value("${name}")
        
  1. @Controller
  2. public class CustomerController {
  3. @Autowired
  4. private BaseDictService baseDictService;
  5. @Autowired
  6. private CustomerService customerService;
  7. //客户来源类别码
  8. @Value("${CUST_SOURCE}")
  9. private String CUST_SOURCE;
  10. //客户行业类别码
  11. @Value("${CUST_INDUSTRY}")
  12. private String CUST_INDUSTRY;
  13. //客户级别类别码
  14. @Value("${CUST_LEVEL}")
  15. private String CUST_LEVEL;
  16. /**
  17. * 跳转显示页面
  18. * @return
  19. */
  20. @RequestMapping("list")
  21. public String list(Model model){
  22. //客户来源
  23. List<BaseDict> sourceList= baseDictService.findBaseDictWithTypeCode(CUST_SOURCE);
  24. model.addAttribute("fromType", sourceList);
  25. //客户行业
  26. List<BaseDict> industryList= baseDictService.findBaseDictWithTypeCode(CUST_INDUSTRY);
  27. model.addAttribute("industryType", industryList);
  28. //客户级别
  29. List<BaseDict> levelList= baseDictService.findBaseDictWithTypeCode(CUST_LEVEL);
  30. model.addAttribute("levelType", levelList);
  31. return "list";
  32. }
  33. }

2、example类的运用

  1. @Autowired
  2. private BaseDictMapper baseDictMapper;
  3. /**
  4. * 需求:根据类别码查询类别
  5. * 参数:String typeCode
  6. * 返回值:List<BaseDict>
  7. */
  8. public List<BaseDict> findBaseDictWithTypeCode(String typeCode) {
  9. // 创建example对象
  10. BaseDictExample example = new BaseDictExample();
  11. //创建criteria对象
  12. //createCriteria是一个list集合,封装查询条件
  13. //where id=3 AND username like ‘‘ and sex=男
  14. //createCriteria.andIdEqualTo(id) === id=3
  15. //createCriteria.andUsernameLike(username) === username like ‘‘
  16. //xml文件使用foreach循环条件,变成一个sql语句
  17. Criteria createCriteria = example.createCriteria();
  18. //设置查询参数 根据类别码查询类别
  19. createCriteria.andDictTypeCodeEqualTo(typeCode);
  20. //执行查询
  21. List<BaseDict> baseList = baseDictMapper.selectByExample(example);
  22. return baseList;
  23. }


四、子父容器

技术分享
1、 如果在service层的applicationContext-service.xml里面配置
<context:component-scan base-package="com.heima"></context:component-scan>
并且将springmvc.xml对controller层的扫描关闭,重新启动会报错,因为此时controller层的@RequestMapping、@RequestParameter等注解都是子容器,而controller类上的@controller是父容器,父容器不能访问子容器,所以报错;;
反过来用springmvc.xml进行全部的sping扫描,是可以的

2、也可以将web.xml里面用listener配置spring配置文件的加载去掉,将applicationContext-*.xml的配置在springmvc的配置中,让子容器去加载父容器配置文件
  1. <servlet>
  2. <servlet-name>springmvc</servlet-name>
  3. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  4. <init-param>
  5. <param-name>contextConfigLocation</param-name>
  6. <param-value>classpath:springmvc.xml,classpath:applicationContext-*.xml</param-value>
  7. </init-param>
  8. <load-on-startup>1</load-on-startup>
  9. </servlet>
此时只有一个容器,所以各种扫描只能出现一次,所以讲springmvc.xml里面对resources.properties的扫描去掉,放在applicationContext-dao.xml里面
<context:property-placeholder location="classpath:*.properties" />































springmvc-crm-ssm整合

标签:dbcc   -o   rate   enable   asi   callback   oid   lock   package   

原文地址:http://www.cnblogs.com/qinhelili/p/6985958.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!