Mybatis配置文件详解

一、目录结构
Mybatis配置文件详解_第1张图片

二、代码详解

1.maven核心配置文件



    4.0.0

    com.mollen.mybatis
    mybatis
    1.0-SNAPSHOT

    
    
        
            org.mybatis
            mybatis
            3.4.5
        
        
        
            mysql
            mysql-connector-java
            5.1.38
        
        
        
            log4j
            log4j
            1.2.17
        
        
        
            junit
            junit
            4.11
        
        
    

    
    
        
        
            
                src/main/java
                
                    **/*.xml
                
            
        
        
	
        
       	    
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                    utf-8
                
            
        
    


2.mybatis核心配置文件:SqlMapConfig.xml



        



    
    
    
    
    
        
        
    

    
    
        
            
            
                
                
                
                
            
        
    

    
    
     	
        
        
        
         
    


3.配置数据源文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis_test
jdbc.username=root
jdbc.password=root

4.映射文件配置:UserDao.xml

 





    
    
        
        
        
        
        
        
        

    

    
    

    
    

    
    

    
    

    
    
        INSERT  INTO
          user
        VALUES
        (
          null,
          #{username},
          #{birthday},
          #{sex},
          #{address}
        )
    

    
    
        DELETE  FROM
          user
        WHERE
          id = #{id}
    

    
    
        UPDATE
          user
        SET
          username = #{username},
          birthday = #{birthday},
          sex = #{sex},
          address = #{address}
        WHERE
          id = #{id}
    


5.Dao接口类:UserDao.java

package com.mollen.dao;

import com.mollen.bean.User;

import java.util.List;

/**
 * @ClassName: UserDao
 * @Auther: Mollen
 * @CreateTime: 2018-10-27  20:19:07
 * @Description:
 */
public interface UserDao {

    /**
     * 1.查询所有
     * @return
     */
    public List findAll();


    /**
     * 2.根据id查询用户(动态执行参数)
     * @return
     */
    public List findUserById(Integer...ids);

    /**
     * 3.根据用户模糊查询
     */
    public List findByName(String username);

    /**
     * 4.根据条件模糊查询
     * @param user
     * @return
     */
    public List findByCondition(User user);

    /**
     * 5.添加一条数据
     * @param user
     */
    public void saveOne(User user);

    /**
     * 6.删除一条数据
     * @param user
     */
    public void deleteOne(User user);

    /**
     * 7.修改用户
     */
    public void updateUser(User user);
}

6.实体类:User.java

package com.mollen.bean;

import java.util.Date;

/**
 * @ClassName: User
 * @Auther: Mollen
 * @CreateTime: 2018-10-29  10:11:48
 * @Description:
 */
public class User {
    private int id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;
	//getter/setter/toString/...
}

原文:https://blog.csdn.net/mollen/article/details/84566676

你可能感兴趣的:(后端)