SSM框架——详细整合教程(Spring+SpringMVC+MyBatisPlus+Maven)

记录SSM 框架整合、使用Maven构建,说M实际上是MybatisPlus、是一种对Mybatis封装好的框架、在不影响Mybatis原生的情况下,进行的一种扩展。

框架支持Freemarker 和 JSP 双View展示(优先找Freemarker)。

项目环境:
JDK : 1.7
SpringMVC : 4.2.5
Spring : 4.2.5
Mybatis Plus : 2.0.5
IntelliJ IDEA : 2016.2.5
LomBok : 1.16.8
Veolcity : 1.7 只用来mybatis反向生成代码
freemarker : 2.3.23

LomBok 是一种能自动生成Get & Set 等方法、大大减少开发时代码量、保持代码简洁。
查看LomBok 操作:https://projectlombok.org/

Github 项目地址:https://github.com/Jandaes/liujilu-ssm

  • Mybatis Plus 自动会引入Mybatis 的jar
  • 使用Maven Tomcat Plugin部署运行 (Intellij IDEA 中如何部署项目到 Tomcat?)
  • 可通过Eclipse 或MyEclipse部署运行、不仅限于IDEA

pom.xml :

    
        liujilu
        
            
            
                org.apache.tomcat.maven
                tomcat7-maven-plugin
                2.2
                
                    UTF-8
                
            
        
    
    
    
        UTF-8
        4.2.5.RELEASE
        4.12
        1.0.18
        1.2.8
        2.0.5
        5.1.38
        1.2.17
        1.7.19
        1.8.8
        1.3.1
    
    
    
    
        org.springframework
        spring-web
        ${spring.version}
        jar
        compile
    
    
    
    

web.xml

    
    
        spring-mvc
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:spring/spring-mvc.xml
        
        1
    
    
        spring-mvc
        /
    
    
    
    
        contextConfigLocation
        classpath:spring/spring.xml,classpath:spring/spring-*.xml
    
    
    
        org.springframework.web.context.ContextLoaderListener
    

    
    
        org.springframework.web.util.IntrospectorCleanupListener
    

从项目根目录/开始拦截所有的请求、全部交给Springmvc管理,包括静态资源(css,js,image),如果项目有引入静态资源也被拦截的话、需要在springmvc.xml加入:

classpath:spring/spring-*.xml : 加载所有spring目录下spring- 开头的xml文件

Spring.xml

    
    
    
    

    
    
        
    

SpringMVC.xml

    
    
        
    
    
    
    
    
    
    
        
        
        
        
    

    
    
        
        
        
        
    
    
    
    
        
        
        
            
                10
                zh_CN
                yyyy-MM-dd
                yyyy-MM-dd
                #.##
            
        
    
    
    
    
        
        
        
        
        
        
        
        
    

/WEB-INF/pages/ : SpringMVC 的返回页面、只返回.jsp结尾的文件
/WEB-INF/views/:Freemarker 的模版页面、只返回.ftl结尾的文件

Freemarker文件后缀可自己编写、比如:*.html

spring-mybatis.xml

    
    
    
        
        
        
        
        
         
        
        
            
                
                
                    
                
            
        
        
        
    
    
    
    
        
        
        
        
        
        
        
        
        
        
    

整合Mybatis plus 配置

SysUser.java

/**
 * 

* 用户信息表 *

* * @since 2017-07-31 */ @TableName("sys_user") @Data @AllArgsConstructor @NoArgsConstructor public class SysUser extends Model { private static final long serialVersionUID = 1L; /** * 编号 */ @TableId(value="id", type= IdType.ID_WORKER) private String id; /** * 姓名 */ private String name; /** * 昵称 */ private String nickname; /** * 邮箱 */ private String email; /** * Q号码 */ private String number; /** * 密码 */ private String password; /** * 创建时间 */ @TableField("create_time") private Date createTime; /** * 最后登录时间 */ @TableField("last_login_time") private Date lastLoginTime; /** * 状态:0 锁定、 1 正常 */ private Integer status; @Override protected Serializable pkVal() { return this.id; } }

@Data : 会自动生成Set & Get方法
@AllArgsConstructor : 会自动生成构造方法
@NoArgsConstructor : 会自动生成无参构造方法
@TableId(type= IdType.ID_WORKER):主键生成策略、查看spring-mybatis.xml

PS : 如果有构造方法的话、必须要写无参构造方法

SysUserController.java

@Controller
@RequestMapping("/")
public class SysUserController {
    private Logger logger = Logger.getLogger(SysUserController.class);

    @Autowired
    private SysUserService userService;

    /**
     * 返回到JSP
     * @return
     */
    @RequestMapping("index")
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView("index");
        logger.info("进来了");
        List userList = userService.selectList(new EntityWrapper());
        for (SysUser user:userList) {
            System.out.println(user.toString());
        }
        mv.addObject("userList",userList);
        return mv;
    }

    /**
     * 返回到FreeMarker
     * @param model
     * @return
     */
    @RequestMapping("/freemarker/index")
    public String index(Model model){
        logger.info("进来了Freemarker 处理器");
        List userList = userService.selectList(new EntityWrapper());
        for (SysUser user:userList) {
            System.out.println(user.toString());
        }
        model.addAttribute("userList",userList);
        return "indexs";
    }
}

MpGenerator.java

/**
 * 

* 代码生成器请勿轻易操作 * 操作时请取消 pom.xml 中的 velocity 注释 *

*/ public class MpGenerator { /** * @param args */ public static void main(String[] args) { AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); gc.setOutputDir("D://cache");//生成存放地址 gc.setFileOverride(true); gc.setActiveRecord(true); gc.setEnableCache(false);// XML 二级缓存 gc.setBaseResultMap(true);// XML ResultMap gc.setBaseColumnList(false);// XML columList gc.setAuthor("D.Yang"); // 自定义文件命名,注意 %s 会自动填充表实体属性! gc.setMapperName("%sMapper"); gc.setXmlName("%sMapper"); gc.setServiceName("%sService"); gc.setServiceImplName("%sServiceImpl"); gc.setControllerName("%sController"); mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setDbType(DbType.MYSQL); dsc.setTypeConvert(new MySqlTypeConvert(){ // 自定义数据库表字段类型转换【可选】 @Override public DbColumnType processTypeConvert(String fieldType) { System.out.println("转换类型:" + fieldType); return super.processTypeConvert(fieldType); } }); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword(""); dsc.setUrl("jdbc:mysql://127.0.0.1:3306/liujilu?characterEncoding=utf8"); mpg.setDataSource(dsc); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setTablePrefix(new String[] {""});// 此处可以修改为您的表前缀 strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略 //strategy.setInclude(new String[]{"sys_order"});//生成指定表 mpg.setStrategy(strategy); // 包配置 PackageConfig pc = new PackageConfig(); pc.setParent("com"); pc.setModuleName("liujilu"); pc.setEntity("model"); mpg.setPackageInfo(pc); // 执行生成 mpg.execute(); } }

该工具类、自动通过连接数据库生成对应的java文件、包括:
Controller、model、Service、ServiceImpl、Mappler、Mapperxml
只要修改配置里面的数据库连接地址、以及修改生成代码的存放地址就可以使用。

具体更多的MybatisPlus方法操作、查看:http://git.oschina.net/baomidou/mybatis-plus

以上代码为主要代码、更多代码查看Github 源码

效果图

SSM框架——详细整合教程(Spring+SpringMVC+MyBatisPlus+Maven)_第1张图片
效果图

后续提交添加、修改、删除等操作

参考网站:http://liujilu.com/

你可能感兴趣的:(SSM框架——详细整合教程(Spring+SpringMVC+MyBatisPlus+Maven))