189 8069 5689

mybatis自动生成代码及使用

mybatis自动生成代码及使用

1.新建maven项目

专注于为中小企业提供成都网站建设、网站设计服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业应城免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了成百上千企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

2.项目基础配置

找到maven工程所在目录下org.eclipse.wst.common.project.facet.core.xml此文件
mybatis自动生成代码及使用

修改成如下内容

mybatis自动生成代码及使用

maven工程下的web.xml文件替换为




pom.xml文件中修改build节点,添加如下内容

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>

3.在pom.xml追加如下内容


    org.mybatis.generator
    mybatis-generator-core
    1.3.2

    org.mybatis.generator
    mybatis-generator-maven-plugin
    1.3.2
    
        
        src/main/resources/generatorConfig.xml
        true
        true
    
    
        
        
            MySQL
            mysql-connector-java
            5.1.34
        
                
    
        
            Generate MyBatis Artifacts
            
                generate
            
        
    

4.在src/main/resources文件下创建generatorConfig.xml,内容如下







    
    
        
        
            
        

        
        
        

        
        
            
            
        

        
        
            
        

        
        
            
        

        
        

5.右键项目--》run As--》Maven build--》输入mybatis-generator:generate,即可创建成功。

mybatis自动生成代码及使用

6.生成代码如下

package com.lymn.it.mapper;

import com.lymn.it.model.User;

public interface UserMapper {
    int deleteByPrimaryKey(Integer userid);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Integer userid);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);

}



  
    
    
    
    
    
    
    
  
  
    userid, username, password, email, phone, status, code
  
  
  
    delete from user
    where userid = #{userid,jdbcType=INTEGER}
  
  
    insert into user (userid, username, password, 
      email, phone, status, 
      code)
    values (#{userid,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
      #{email,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{status,jdbcType=VARCHAR}, 
      #{code,jdbcType=VARCHAR})
  
  
    insert into user
    
      
        userid,
      
      
        username,
      
      
        password,
      
      
        email,
      
      
        phone,
      
      
        status,
      
      
        code,
      
    
    
      
        #{userid,jdbcType=INTEGER},
      
      
        #{username,jdbcType=VARCHAR},
      
      
        #{password,jdbcType=VARCHAR},
      
      
        #{email,jdbcType=VARCHAR},
      
      
        #{phone,jdbcType=VARCHAR},
      
      
        #{status,jdbcType=VARCHAR},
      
      
        #{code,jdbcType=VARCHAR},
      
    
  
  
    update user
    
      
        username = #{username,jdbcType=VARCHAR},
      
      
        password = #{password,jdbcType=VARCHAR},
      
      
        email = #{email,jdbcType=VARCHAR},
      
      
        phone = #{phone,jdbcType=VARCHAR},
      
      
        status = #{status,jdbcType=VARCHAR},
      
      
        code = #{code,jdbcType=VARCHAR},
      
    
    where userid = #{userid,jdbcType=INTEGER}
  
  
    update user
    set username = #{username,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR},
      phone = #{phone,jdbcType=VARCHAR},
      status = #{status,jdbcType=VARCHAR},
      code = #{code,jdbcType=VARCHAR}
    where userid = #{userid,jdbcType=INTEGER}
  

package com.lymn.it.model;

public class User {
    private Integer userid;

    private String username;

    private String password;

    private String email;

    private String phone;

    private String status;

    private String code;

    public Integer getUserid() {
        return userid;
    }

    public void setUserid(Integer userid) {
        this.userid = userid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email == null ? null : email.trim();
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone == null ? null : phone.trim();
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status == null ? null : status.trim();
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code == null ? null : code.trim();
    }
}

8.参照SSM框架搭建之传统方式(一)博客中的service、controller层进行测试


名称栏目:mybatis自动生成代码及使用
URL标题:http://cdxtjz.cn/article/ishopd.html

其他资讯