首页 > 代码库 > springmvc-crm-ssm整合
springmvc-crm-ssm整合
一、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,对配置文件进行整理
需要修改的地方:
<?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>
<!-- classPathEntry:数据库的JDBC驱动的jar包地址 -->
<classPathEntry
location="G:\mysql-connector-java-5.1.7-bin.jar" />
<context id="caigouTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="false" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg"
userId="yycg"
password="yycg">
</jdbcConnection> -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/crm"
userId="root"
password="123">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="com.heima.domain"
targetProject="crm_ssm">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:自动mapper的位置 -->
<sqlMapGenerator targetPackage="com.heima.mapper"
targetProject="crm_ssm">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.heima.mapper" implementationPackage="com.heima.mapper"
targetProject="crm_ssm">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<table tableName="base_dict" ></table>
<table tableName="customer" ></table>
<!-- <table schema="" tableName="userjd" />
<table schema="" tableName="usergys" />
<table schema="" tableName="dictinfo" />
<table schema="" tableName="dicttype" />
<table schema="" tableName="basicinfo" /> -->
</context>
</generatorConfiguration>
操作:
结果:
2、代码逆向工程
①、需要的jar包:
②、导入逆向工程配置文件:generatorConfig-base.xml
按照之前插件逆向工程那样的修改,但是targetProject应该都等于".\src"
<?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>
<!-- classPathEntry:数据库的JDBC驱动的jar包地址 -->
<classPathEntry
location="G:\mysql-connector-java-5.1.7-bin.jar" />
<context id="caigouTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="false" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg"
userId="yycg"
password="yycg">
</jdbcConnection> -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/crm"
userId="root"
password="123">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="com.heima.domain"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:自动mapper的位置 -->
<sqlMapGenerator targetPackage="com.heima.mapper"
targetProject=".\src">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.heima.mapper" implementationPackage="com.heima.mapper"
targetProject=".\src">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<table tableName="base_dict" ></table>
<table tableName="customer" ></table>
<!-- <table schema="" tableName="userjd" />
<table schema="" tableName="usergys" />
<table schema="" tableName="dictinfo" />
<table schema="" tableName="dicttype" />
<table schema="" tableName="basicinfo" /> -->
</context>
</generatorConfiguration>
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
public class SqlSGen {
public static void main(String[] args) throws Exception {
SqlSGen();
}
private static void SqlSGen() throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("src/generatorConfig-base.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
callback, warnings);
myBatisGenerator.generate(null);
}
}
三、项目知识点
1、用注解引入properties文件到java代码中
①、要在springmvc.xml中配置如下
<context:property-placeholder location="classpath:resource.properties" />
②、注解:@Value("${name}")
@Controller
public class CustomerController {
@Autowired
private BaseDictService baseDictService;
@Autowired
private CustomerService customerService;
//客户来源类别码
@Value("${CUST_SOURCE}")
private String CUST_SOURCE;
//客户行业类别码
@Value("${CUST_INDUSTRY}")
private String CUST_INDUSTRY;
//客户级别类别码
@Value("${CUST_LEVEL}")
private String CUST_LEVEL;
/**
* 跳转显示页面
* @return
*/
@RequestMapping("list")
public String list(Model model){
//客户来源
List<BaseDict> sourceList= baseDictService.findBaseDictWithTypeCode(CUST_SOURCE);
model.addAttribute("fromType", sourceList);
//客户行业
List<BaseDict> industryList= baseDictService.findBaseDictWithTypeCode(CUST_INDUSTRY);
model.addAttribute("industryType", industryList);
//客户级别
List<BaseDict> levelList= baseDictService.findBaseDictWithTypeCode(CUST_LEVEL);
model.addAttribute("levelType", levelList);
return "list";
}
}
2、example类的运用
@Autowired
private BaseDictMapper baseDictMapper;
/**
* 需求:根据类别码查询类别
* 参数:String typeCode
* 返回值:List<BaseDict>
*/
public List<BaseDict> findBaseDictWithTypeCode(String typeCode) {
// 创建example对象
BaseDictExample example = new BaseDictExample();
//创建criteria对象
//createCriteria是一个list集合,封装查询条件
//where id=3 AND username like ‘‘ and sex=男
//createCriteria.andIdEqualTo(id) === id=3
//createCriteria.andUsernameLike(username) === username like ‘‘
//xml文件使用foreach循环条件,变成一个sql语句
Criteria createCriteria = example.createCriteria();
//设置查询参数 根据类别码查询类别
createCriteria.andDictTypeCodeEqualTo(typeCode);
//执行查询
List<BaseDict> baseList = baseDictMapper.selectByExample(example);
return baseList;
}
四、子父容器
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的配置中,让子容器去加载父容器配置文件
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml,classpath:applicationContext-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
此时只有一个容器,所以各种扫描只能出现一次,所以讲springmvc.xml里面对resources.properties的扫描去掉,放在applicationContext-dao.xml里面
<context:property-placeholder location="classpath:*.properties" />
null
springmvc-crm-ssm整合
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。