springBoot 整合 mybatis+Oracle

您所在的位置:网站首页 spring操作mybits springBoot 整合 mybatis+Oracle

springBoot 整合 mybatis+Oracle

#springBoot 整合 mybatis+Oracle| 来源: 网络整理| 查看: 265

现在比较流行的操作数据库操作层框架Mybatis,下面我们就来看看Springboot如何整合mybatis, 之前一直在用xml形式写sql,这次依然用xml的方式感觉这种还是比较灵活方便。

  添加mybatis关键就是要引入mybatis-spring-boot-starter到pom文件中,如果你用MySQL,那就引入MySQL的pom文件,这里我用Oracle,淡然要引入Oracle的依赖了。添加完成mybatis和Oracle 在pom.xml 文件中的引入。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21               oracle         ojdbc6         11.2.0.3                     org.springframework.boot         spring-boot-starter-jdbc                           org.mybatis.spring.boot         mybatis-spring-boot-starter         1.3.1                     com.alibaba         druid         1.1.0      

  

注意:

如果你用的是Oracle数据库在pom.xml中有可能会报这个错Missing artifact oracle:ojdbc6:jar 因为Oracle的ojdbc.jar是收费的,所以maven的中央仓库没有这个资源,只能配置本地仓库才能加载到项目中去。

配置application.properties

1 2 3 4 5 6 7 8 9 10 11 mybatis.config-locations=classpath:mybatis/mybatis-config.xml mybatis.mapper-locations=classpath:mapper/*.xml #oracle database  jdbc spring.datasource.url=jdbc:oracle:thin:@//localhost:1521/orcl spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver spring.datasource.max-idle=10 spring.datasource.max-wait=10000 spring.datasource.min-idle=5 spring.datasource.initial-size=5

  

指定了mybatis的配置地址,你可以在mybatis/mybatis-config.xml中添加一些其他配置

完成之后你就可以去mapper文件夹下自由潇洒的去写sql去了

我们来测试一下看看是否成功,去写个sql试试效果怎么样?就返回用户的一些基本信息吧。实现代码如下:

Controller层:

1 2 3 4 5 6 7 8 9 10 11 @RestController public class UserController {     @Autowired     private UserService userService;       @RequestMapping(value = "/user", method = RequestMethod.GET)     public User findUserByName(@RequestParam(value = "userName", required = true) String userName) {         return userService.findUserByName(userName);     }   }

  

Service层:只写方法具体实现在UserServiceImpl类中

1 2 3 4 5 6 @Service public interface UserService {       User findUserByName(String userName);       }

  

Service层的实现类,实现UserService中的方法

1 2 3 4 5 6 7 8 9 10 11 12 @Component public class UserServiceImpl implements UserService {     @Autowired     private UserDao userDao;       @Override     public User findUserByName(String userName) {         User user = userDao.findUserByName(userName);         return  user;     }   }

  

Dao层 注意要添加@Mapper注解,否则会报错的

1 2 3 4 @Mapper   public interface UserDao {     User findUserByName(@Param("userName") String userName); }

  

实体类

1 2 3 4 5 6 public class User {     private String code;     private String username;     private String name;     //get/set方法省略 }

  

userMapper.xml  要注意namespace能否正确跳转(路径是否正确)

1 2 3 4 5 6 7 8 9              select code, user_name as username, name         from tb_user_info           where name=#{userName}     

  这些都完成之后,那么接下来就是见证奇迹的时刻了,启动项目,浏览器输入地址显示如下

数据库中查询结果如下

成功整合了mybatis和Oracle!



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3