mybatis初次尝试

记录一下自己学习mybatis的过程

  整个工程项目目录如下:

  mybatis初次尝试_第1张图片

  pom.xml配置如下:

  



    4.0.0

    lpp
    mybatisTest
    1.0-SNAPSHOT

    
        
            org.mybatis
            mybatis
            3.1.1
        

        
            mysql
            mysql-connector-java
            5.1.38
        

        
            junit
            junit
            4.11
            test
        

    


  

  mybatis的配置文件如下:

DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

    
        
    
    default="development">
        
            
            
                
                
                
                
            
        
    
    
        
    

 

创建一个实体类,car.java

package com.lpp.test.po;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lpp
 * Date: 2018-04-16
 * Time: 11:06
 */
public class Car {
    int id;
    String car_name;
    int maxSpeed;

    public Car() {
    }

    public Car(int id, String carName, int maxSpeed) {
        this.id = id;
        this.car_name = car_name;
        this.maxSpeed = maxSpeed;
    }

    public int getId() {
        return id;
    }

    public String getCar_name() {
        return car_name;
    }

    public int getMaxSpeed() {
        return maxSpeed;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String car_name) {
        this.car_name = car_name;
    }

    public void setMaxSpeed(int maxSpeed) {
        this.maxSpeed = maxSpeed;
    }

    @Override
    public String toString() {
        return "Car{" +
                "id=" + id +
                ", name='" + car_name + '\'' +
                ", maxSpeed=" + maxSpeed +
                '}';
    }
}

 

创建carMapper.xml


DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

    
    
    

 

创建carMapper接口 carMapper.java

package com.lpp.test.impl;

import com.lpp.test.po.Car;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lpp
 * Date: 2018-04-17
 * Time: 19:44
 */
public interface carMapper {
    public Car findCarbyId(int id);
    public Car findCarbyIdAndName(@Param("id") int id, @Param("car_name") String car_name);
    //@Select("SELECT * FROM t_car WHERE id = #{id} AND maxSpeed = #{maxSpeed}")
    public Car findCarbyIdAndSpeed(Car car);
}

前期工作已经完成了。现在创建测试类

CarDaoImplTest.java
package com.lpp.test;

import com.lpp.test.impl.carMapper;
import com.lpp.test.po.Car;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lpp
 * Date: 2018-04-16
 * Time: 11:21
 */
public class CarDaoImplTest {

    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void init() throws IOException {
        String resource = "config/SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Test
    public void testFindUserById(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Car car = null;
        Car car1 = new Car();
        try {
            //car = sqlSession.selectOne("test.findCarbyId",1);
            carMapper carMapper = sqlSession.getMapper(carMapper.class);
            //car = carMapper.findCarbyId(1);
            //car = carMapper.findCarbyIdAndName(1,"aodi");
            car1.setId(1);
            //car1.setName("aodi");
            car1.setMaxSpeed(280);
            car = carMapper.findCarbyIdAndSpeed(car1);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
        System.out.println(car);

    }
}

右击运行测试,得到结果

 查询出数据 大功告成。

最后附上gitee的地址:https://gitee.com/LL7_s/personal_test_code_sorting/tree/master/mybatisTest

可以自行查阅。新手上路,请各位多多关照。

转载于:https://www.cnblogs.com/lpl1/p/8950731.html

你可能感兴趣的:(mybatis初次尝试)