Spring boot + mybatis + orcale实战(干货)

废话少说,直接上步骤:

第一步:安装好IDEA(此处省略)

第二步:在IDEA新建springboot工程

第三步:在springboot工程的pom.xml添加oracle和mybait依赖

<!-- oracle -->
<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>11.2.0</version>
</dependency>
<!-- jdbc -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.0</version>
</dependency>

 

第四步:在resources新建文件application.yml

spring:
    datasource:
    driver-class-name: oracle.jdbc.OracleDriver
    url: jdbc:oracle:thin:@192.168.11.162:1521:dbtest
    username: li
    password: li
mybatis:
  mapper-locations: classpath:mapper/*.xml

  

第五步:新建java/Bean/XXXVO.java  模型

public class UserVO {

    private  Integer userid;
    private  Integer age;
    private  Integer sex;

   //getter and setter
}

  

第六步:新建java/Mapper/XXXMapper.java  接口

@Mapper
public interface AccountMapper {

    void updateUserInfo(UserVO userVO);  //更新用户信息

    public List<UserVO> selsetUserList(); //获取用户列表

    public UserVO getUserInfoById(Integer userid);//获取会员信息
}

  

第七步:新建java/Service/XXXService.java   服务

@Repository
@Service
public class AccountService {

    @Autowired
    private AccountMapper accountMapper;


   public  void updateUserInfo(UserVO userVO) {
        accountMapper.updateUserInfo(userVO);
    }


    public List<UserVO> selsetUserList(){

        return accountMapper.selsetUserList();
    }

  public UserVO getUserInfoById(Integer id){

      return accountMapper.getUserInfoById(id);
  }
}

  

第八步:新建resources/mapper上新建XXXMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="com.cwn.springboot.Mapper.AccountMapper">

<update id="updateUserInfo" parameterType="com.cwn.springboot.bean.UserVO">

update test_users a
        <set>
        <if test="firstname!=null"> a.firstname    =#{firstname}, </if>
        <if test="lastname!=null">a.lastname     =#{lastname}, </if>
        <if test="sex!=null">a.sex          =#{sex}, </if>
        <if test="age!=null">a.age          =#{age}, </if>
        <if test="email!=null"> a.email        =#{email}, </if>
        <if test="usersigninfo!=null">a.usersigninfo =#{usersigninfo}</if>
        </set>
 where a.userid=#{userid}
</update>


<select id="selsetUserList" resultType="com.cwn.springboot.bean.UserVO">
select a.userid,a.username,a.email  from test_users a where a.status=1
</select>


<select id="getUserInfoById" resultType="com.cwn.springboot.bean.UserVO">
 select * from test_users where userid=#{userid}
</select>
</mapper>

  

  

 第九步:在SpringbootApplicationTests上测试

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Springboot01Application.class)
public class SpringbootApplicationTests {

    @Autowired
    private AccountService accountService;


    @Test
    public void testSelecUser(){
      UserVO users= accountService.getUserInfoById(1);

      System.out.println(users.getUserid());
      System.out.println(users.getAge());
      System.out.println(users.getSex());
    }

}

  



原文地址:https://www.cnblogs.com/ningshare/p/10649145.html