SpringMvc+Mybatis 框架搭建

本文承接上一篇【idea使用maven搭建springmvc】

开篇:在main/resources下新建dbconfig.properties、spring.xml、spring-mybatis.xml,内容如下:

driverClassName=com.mysql.jdbc.Driver
validationQuery=SELECT 1
jdbc_url=jdbc:mysql://localhost:3306/dangdb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
jdbc_username=root
jdbc_password=root
dbconfig.properties


       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    
    
    
    package="com.dang.service" />
spring.xml

"
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">

    
    
    
    
    class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    

    
    
    class="org.mybatis.spring.SqlSessionFactoryBean">
        
        
        
        
    
    
    class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        
        
    

    
    
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
    

    
    

    
    
        
            
            
            
            
            
            
            
            
            
            
            

            
            
            
            
            

            
        
    
    
        
        
    


    
    class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
    
    class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
        
            
                com.dang.service.*
            
        
    
    
        
    

spring-mybatis.xml

 

1、在com.dang目录下新建文件夹如下图:

SpringMvc+Mybatis 框架搭建_第1张图片

2、根据mybatis-generator工具生成数据表的相关代码和sql映射文件

打开http://mvnrepository.com/ 搜索mybatis-generator-core,这里我选择了最新版的1.3.5

SpringMvc+Mybatis 框架搭建_第2张图片

解压后找到lib目录,把当前使用的mysql jar包放在此目录下,并新建generator.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 location="mysql-connector-java-5.1.39.jar" />
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        commentGenerator>
        
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/dangdb" userId="root" password="root">
        jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        javaTypeResolver>
        
        <javaModelGenerator targetPackage="com.dang.model" targetProject="src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        javaModelGenerator>
        
        <sqlMapGenerator targetPackage="com.dang.mapping" targetProject="src">
            <property name="enableSubPackages" value="true" />
        sqlMapGenerator>
        
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.dang.dao" targetProject="src">
            <property name="enableSubPackages" value="true" />
        javaClientGenerator>
        
        <table tableName="sysuser" domainObjectName="SysUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
    context>
generatorConfiguration>
generator.xml

生成方法:选中lib文件夹【shift+右键】,选择“在此处打开命令窗口”打开cmd窗口,输入

java -jar mybatis-generator-core-1.3.5.jar -configfile generator.xml –overwrite

SpringMvc+Mybatis 框架搭建_第3张图片

把生成的文件放入项目相应的文件夹

3、在service文件夹下新建SysUserService接口,impl下新建SysUserServiceImpl类

SysUserService内容如下: 

package com.dang.service;

import com.dang.model.SysUser;
import java.util.List;

/**
 * Created by dang on 2017/1/5.
 */

public interface SysUserService {

    /**
     * 添加
     * @param sysUser
     */
    int addSysUser(SysUser sysUser);

    /**
     * 删除
     * @param id
     */
    int deleteBySysUserId(Integer id);

    /**
     * 修改
     * @param sysUser
     */
    int updateBySysUserId(SysUser sysUser);

    /**
     * 根据用户id获取用户
     * @param sysUserId
     * @return
     */
    SysUser getSysUserById(Integer sysUserId);

    List getAll();
}
SysUserService

SysUserServiceImpl内容如下:  

package com.dang.service.impl;

import com.dang.dao.SysUserMapper;
import com.dang.model.SysUser;
import com.dang.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by dang on 2017/1/5.
 */
/**
 * 使用@Service注解将SysUserServiceImpl类标注为一个service
 * service的id是sysUserService
 */
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService{

    /**
     * 使用@Autowired注解标注sysUserMapper变量,
     * 当需要使用SysUserMapper时,Spring就会自动注入SysUserMapper
     */
    @Autowired
    private SysUserMapper sysUserMapper; //注入dao

    @Override
    public int addSysUser(SysUser sysUser) {
       return sysUserMapper.insert(sysUser);
    }

    @Override
    public int deleteBySysUserId(Integer id) {
        return sysUserMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateBySysUserId(SysUser sysUser) {
        return sysUserMapper.updateByPrimaryKey(sysUser);
    }

    @Override
    public SysUser getSysUserById(Integer sysUserId) {
        return sysUserMapper.selectByPrimaryKey(sysUserId);
    }

    @Override
    public List getAll() {
        return sysUserMapper.getAll();
    }

}
SysUserServiceImpl

ps: getAll()方法因为在生成映射和sql语句时是没有的,要在SysUserMapper.xml文件中添加上去

SpringMvc+Mybatis 框架搭建_第4张图片

并在dao文件夹SysUserMapper接口中加入此项,完整内容如下:

package com.dang.dao;

import com.dang.model.SysUser;

import java.util.List;

/**
 * 各方法名均要对应SysUserMapper.xml中各标签的【id】值
 */
public interface SysUserMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(SysUser record);

    int insertSelective(SysUser record);

    SysUser selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(SysUser record);

    int updateByPrimaryKey(SysUser record);

    List getAll();
}
SysUserMapper

4、在controller文件夹添加SysUserController的java文件,内容如下:

package com.dang.controller;

import com.dang.model.SysUser;
import com.dang.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * Created by dang on 2017/1/5.
 */
@Controller
@RequestMapping(value = "sysuser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String list(ModelMap modelMap) {
        List list = sysUserService.getAll();
        modelMap.addAttribute("list", list);
        return "sysuser/list";
    }
}
SysUserController

在webapp\WEB-INF\View 文件夹下新建sysuser/list.jsp,内容如下:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: dang
  Date: 2017/1/5
  Time: 15:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    list



        
ID 用户名 密码
${list.id} ${list.username} ${list.userpwd}
list.jsp

 

ps:添加、修改的一些操作也大致如此

运行项目,打开 http://localhost:8080/sysuser/list

SpringMvc+Mybatis 框架搭建_第5张图片

运行成功,项目配置完成。

整个项目结构如下:

SpringMvc+Mybatis 框架搭建_第6张图片

 

mybatis-generator下载

你可能感兴趣的:(SpringMvc+Mybatis 框架搭建)