手把手实战:eclipse 搭建 SpringMvc+mybatis 框架环境

说明

    spring mvc的环境在“ 手把手实战:eclipse搭建SpringMvc框架环境 ”中已经搭建完成,那么接下来此会在此基础上把mybatis添加进去(前一章由于有很多截图使得篇幅看起来很长,在这章我就不再粘一遍了。我只是把它的代码拿过来,改个工程的名字来做这篇例子的)

环境准备

    eclipse
    jdk 1.8
    tomcat 7
    mysql 5


添加mybatis所需要的jar包及其他包

手把手实战:eclipse 搭建 SpringMvc+mybatis 框架环境

添加mybatis所需要的相关配置xml 

    在“ 手把手实战:eclipse搭建SpringMvc框架环境 ”中有提过,我在web.xml中已经配置好了所有src下的spring - *。xml都会被我初始化,所以我这里的命名为spring-applicationContext。 XML。
其内容为

<?xml version =“1.0”encoding =“UTF-8”?>
    xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance
    xmlns:context = http://www.springframework.org/schema/context
    xmlns:jdbc = http://www.springframework.org/schema/jdbc 
    xmlns:jee = http://www.springframework.org/schema/jee
    xmlns:tx = http://www.springframework.org/schema/tx
    xmlns:aop = http://www.springframework.org/schema/aop
    xmlns:mvc = http://www.springframework.org/schema/mvc
    xmlns:util = http://www.springframework.org/schema/util
    xmlns:jpa = http://www.springframework.org/schema/data/jpa
    xsi:schemaLocation =
    <! - 开启注解 - >
   <MVC:注释驱动/>  
    <! - 扫描哪些包 - >
   <context:component-scan base-package = “com.hlm” />
   
<! - 配置数据源 - >
<bean id = “dataSource”   class = “org.springframework.jdbc.datasource.DriverManagerDataSource” >
    <property name = “username” value = “hlm” />
    <property name = “password” value = “admin” />
    <property name = “driverClassName”   value = “com.mysql.jdbc.Driver” />
    <property name = “url” value = “jdbc:mysql:// localhost:3306 / hlm_test/>       
</豆>
<! - 配置会话工厂 - >
<bean id = “ssf”
class = “org.mybatis.spring.SqlSessionFactoryBean” >
    <property name = “dataSource” ref = “dataSource” >
    </属性>
    <! - 加载mybatis的映射文件 - >
    <property name = “mapperLocations”
           value = “classpath:com / hlm / sql / *。xml” >
    </属性>
</豆>
<! - 扫描接口包 - >
<bean class = “org.mybatis.spring.mapper.MapperScannerConfigurer” >
    <property name = “basePackage” value = “com.hlm.dao” />
    <property name = “sqlSessionFactoryBeanName” value = “ssf” />
</豆>
</豆>

    数据源配的是我本地的mysql库,这个我就不详细说了。下面的两项配置都是mybatis有关的,一个是mapper的xml的位置,另一个是mybatis会自动装配的dao接口的包位置。
    接下来要创建一个对应数据库的实体类和一个DAO接口。位置如下

手把手实战:eclipse 搭建 SpringMvc+mybatis 框架环境

com.hlm.bo;
import java.io.Serializable;
import java.util.Date;
/ **
 * <p>
 * <ul> 用户:对应hlm_users表的实体</ ul>
 * <li> id:主键</ li>
 * <li> userCode:用户唯一识别码</ li>
 * <li> userName:用户姓名</ li>
 * <li> 性别:性别</ li>
 * <li> 令牌:注册令牌,手机或邮箱</ li>
 * <li> tokenType:令牌类型.0为手机,1为邮箱</ li>
 * <li> createTime:注册时间</ li>
 * <li> 密码:密码</ li>
 * </ p>
 * @author hlm
 *
 * /
公共 用户 实现 Serializable {
     private static final long serialVersionUID = -3452179176154022411L;
     私人 ID ;
     private String userCode ;
     私人 字符串 用户名 ;
     私人 整数 ;
     私人 字符串 令牌 ;
     私人 整数 tokenType ;
     私人 日期 createTime ;
     私人 字符串 密码 ;
     
     public Users(){
           
     }    
     public Long getId(){
           返回 ID ;
     }
     public void setId(Long id ){
           这个id = id ;
     }
     public String getUserCode(){
           返回 userCode ;
     }
     public void setUserCode(String userCode ){
           这个userCode = userCode ;
     }
     public String getUserName(){
           返回 用户名;
     }
     public void setUserName(String userName ){
           这个userName = userName ;
     }
     public Integer getSex(){
           返回 性别;
     }
     public void setSex(Integer sex ){
           这个性别 = 性别;
     }
     public String getToken(){
           返回 令牌;
     }
     public void setToken(String token ){
           这个标记 = 标记;
     }
     public Integer getTokenType(){
           返回 tokenType ;
     }
     public void setTokenType(Integer tokenType ){
           这个tokenType = tokenType ;
     }
     public Date getCreateTime(){
           return createTime ;
     }
     public void setCreateTime(Date createTime ){
           这个createTime = createTime ;
     }
     public String getPassword(){
           返回 密码;
     }
     public void setPassword(String password ){
           这个密码 = 密码;
     }
           
}
com.hlm.dao;
import com.hlm.bo.Users;
公共 接口 UsersDao {
     / **
      *创建用户
      * @param bo
      * /
     public void createUsers(Users bo );
     
}

    另外再创建一个包com.hlm.sql来存放* Mapper.xml,也就是前面配置文件中有配置的

手把手实战:eclipse 搭建 SpringMvc+mybatis 框架环境

手把手实战:eclipse 搭建 SpringMvc+mybatis 框架环境

手把手实战:eclipse 搭建 SpringMvc+mybatis 框架环境

    UserMapper.xml中内容如下,在此我只是简单地写了一条新增的语句。该ID对应我usersDao.java接口中的新增方法名。

<?xml version =“1.0”encoding =“UTF-8”?> 
<!DOCTYPE mapper PUBLIC“ - // ibatis.apache.org // DTD Mapper 3.0 // EN”     
<mapper namespace = “com.hlm.dao.UsersDao” >
<insert id = “createUsers” parameterType = “com.hlm.bo.Users” >
    插入到hlm_users(id,user_code,user_name,
                       性别,令牌,CREATE_TIME,密码,
                                       token_type)
    值(#{ID},{#} USERCODE,#{USERNAME},{#}性别,#{令牌},{#} createTime,#{密码},{#} tokenType)
</插入>
</映射器>

    最后我们改一下原来的UsersController.java

包com.hlm.controller;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.hlm.bo.Users;
import com.hlm.command.UsersCommand;
import com.hlm.dao.UsersDao;

@Controller
public class UsersController { @Resource private UsersDao usersDao; @RequestMapping(“/ sigup.do”)                                                                 public ModelAndView sigup(HttpServletRequest req){  ModelAndView mv = new ModelAndView();







 
UsersCommand cmd = new UsersCommand(“小明”,0,“[email protected]”,1,“123456”); 用户bo =新用户(); BeanUtils.copyProperties(cmd,bo); bo.setUserCode(bo.getUserName()+ “1”); usersDao.createUsers(BO); req.getSession()。setAttribute(“user”,bo); mv.addObject(“password”,bo.getPassword()); mv.addObject(“userName”,bo.getUserName()); 的System.out.println(bo.getPassword()); mv.setViewName( “索引”);   返回mv; } }
   












    老样子,部署好tomcat后启动,启动好后打开浏览器输入网址:http:// localhost:8080 / springmvcmybatis /

手把手实战:eclipse 搭建 SpringMvc+mybatis 框架环境

    点击后

手把手实战:eclipse 搭建 SpringMvc+mybatis 框架环境

    数据库多了一条数据

手把手实战:eclipse 搭建 SpringMvc+mybatis 框架环境


题外话

    我知道有些同学在网上看到这个mybatis的xml配置文件都叫applicationContext.xml并不像我这里叫spring-applicationContext.xml。然后,他们会在web.xml里多出如下的配置语句

手把手实战:eclipse 搭建 SpringMvc+mybatis 框架环境

    当然,这也是可以,只要能告诉web.xml中哪些XML文件是需要读到的就行(其实这两种方式虽然效果一样,但实质上是有区别的,好学的同学可以搜一下相关的答案,我这里就不多说了)。但也有些同学采用上面这种方式会遇到问题,看到我上面的大红框里面有两个小红框没,这两个是必需的。有些同学从别年制的时候可能只复制了第一个框中的,第二个框中的就没复制,导致的MyBatis的配置文件没有生效,启动tomcat的的时候就可能遇到DAO这个豆没找到了,因为刀的bean的在mybatis的配置文件中定义生成方式的。

最后附上数据表建表语句


CREATE TABLE`hlm_users`(
  `id` bigint(20)NOT NULL AUTO_INCREMENT COMMENT'主键',
  `user_code` varchar(40)NOT NULL COMMENT'用户唯一识别码',
  `user_name` varchar(50)NOT NULL COMMENT'用户姓名”,
  `sex` INT(3)DEFAULT NULL COMMENT '性别',
  `token` VARCHAR(40)NOT NULL COMMENT '注册令牌,手机或邮箱',
  `create_time`日期DEFAULT NULL COMMENT '注册时间',
  ` token_type` int(3)NOT NULL COMMENT'令牌类型.0为手机,1为邮箱',
  `password` varchar(30)NOT NULL COMMENT'密码',
  PRIMARY KEY(`id`,`user_code`)
)ENGINE = InnoDB AUTO_INCREMENT = 4 DEFAULT CHARSET = utf8

git代码地址:https://github.com/mottohlm/springmvcm ybatis


20180527