springboot中配置mybatis连接postgresql

https://blog.csdn.net/y_qc_lookup/article/details/80178545

 

springboot中配置mybatis连接postgresql

置顶 Dylan's 2018-05-03 15:49:46  41415  收藏 8

分类专栏: java 文章标签: springboot mybatis postgresql xml

版权

        最近在使用springboot用于搭建程序后台的框架,与之前ssm,ssh等框架相比,搭建简单,只需下载eclipse或其他插件进行安装即可。

    整体结构

    

    1.在pom.xml中加入配置

 
  1. <!-- 加载postgresql驱动 -->

  2. <dependency>

  3. <groupId>org.postgresql</groupId>

  4. <artifactId>postgresql</artifactId>

  5. <scope>runtime</scope>

  6. </dependency>

  7. <!-- 加载jdbc连接数据库 -->

  8. <dependency>

  9. <groupId>org.springframework.boot</groupId>

  10. <artifactId>spring-boot-starter-jdbc</artifactId>

  11. </dependency>

  12. <!-- 加载mybatis jar包 -->

  13. <dependency>

  14. <groupId>org.mybatis.spring.boot</groupId>

  15. <artifactId>mybatis-spring-boot-starter</artifactId>

  16. <version>1.3.2</version>

  17. </dependency>

2.resources/application.properties中加入数据库配置链接

 
  1. ### postgresql config ###

  2. spring.datasource.url=jdbc:postgresql://192.168.1.1:5432/postgre

  3. spring.datasource.username=postgre

  4. spring.datasource.password=123456

  5. spring.datasource.driver-class-name=org.postgresql.Driver

3.编写model模型

 
  1. package com.main.model;

  2.  
  3. import java.io.Serializable;

  4.  
  5. public class UserEntity implements Serializable {

  6. private static final long serialVersionUID = 1L;

  7. private Long id;

  8. private String userName;

  9. private String passWord;

  10. private String userSex;

  11. private String nickName;

  12.  
  13. public UserEntity() {

  14. super();

  15. }

  16.  
  17. public UserEntity(String userName, String passWord, String userSex,String nickName) {

  18. super();

  19. this.passWord = passWord;

  20. this.userName = userName;

  21. this.userSex = userSex;

  22. this.nickName = nickName;

  23. }

  24.  
  25. public Long getId() {

  26. return id;

  27. }

  28.  
  29. public void setId(Long id) {

  30. this.id = id;

  31. }

  32.  
  33. public String getUserName() {

  34. return userName;

  35. }

  36.  
  37. public void setUserName(String userName) {

  38. this.userName = userName;

  39. }

  40.  
  41. public String getPassWord() {

  42. return passWord;

  43. }

  44.  
  45. public void setPassWord(String passWord) {

  46. this.passWord = passWord;

  47. }

  48.  
  49. public String getUserSex() {

  50. return userSex;

  51. }

  52.  
  53. public void setUserSex(String userSex) {

  54. this.userSex = userSex;

  55. }

  56.  
  57. public String getNickName() {

  58. return nickName;

  59. }

  60.  
  61. public void setNickName(String nickName) {

  62. this.nickName = nickName;

  63. }

  64.  
  65. public static long getSerialversionuid() {

  66. return serialVersionUID;

  67. }

  68. @Override

  69. public String toString() {

  70. // TODO Auto-generated method stub

  71. return "userName " + this.userName + ", pasword " + this.passWord + ", nickName " + this.nickName+ ", userSex " + this.userSex;

  72. }

  73. }

4.编写spring mapper。

 
  1. public interface UserMapper {

  2. List<UserEntity> getAll();

  3.  
  4. UserEntity getOne(Long id);

  5.  
  6. void insert(UserEntity user);

  7.  
  8. void update(UserEntity user);

  9.  
  10. void delete(Long id);

  11. }

5.mybatis中的xml配置文件

 
  1. <?xml version="1.0" encoding="UTF-8" ?>

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

  3. <mapper namespace="com.main.mapper.UserMapper" >

  4. <resultMap id="BaseResultMap" type="com.main.model.UserEntity" >

  5. <id column="id" property="id" jdbcType="BIGINT" />

  6. <result column="userName" property="userName" jdbcType="VARCHAR" />

  7. <result column="passWord" property="passWord" jdbcType="VARCHAR" />

  8. <result column="user_sex" property="userSex" jdbcType="VARCHAR"/>

  9. <result column="nick_name" property="nickName" jdbcType="VARCHAR" />

  10. </resultMap>

  11.  
  12. <sql id="Base_Column_List" >

  13. id, userName, passWord, user_sex, nick_name

  14. </sql>

  15.  
  16. <select id="getAll" resultMap="BaseResultMap" >

  17. SELECT

  18. <include refid="Base_Column_List" />

  19. FROM test.userentity

  20. </select>

  21.  
  22. <select id="getOne" parameterType="java.lang.Long" resultMap="BaseResultMap" >

  23. SELECT

  24. <include refid="Base_Column_List" />

  25. FROM test.userentity

  26. WHERE id = #{id}

  27. </select>

  28.  
  29. <insert id="insert" parameterType="com.main.model.UserEntity" >

  30. INSERT INTO

  31. test.userentity

  32. (userName,passWord,user_sex)

  33. VALUES

  34. (#{userName}, #{passWord}, #{userSex})

  35. </insert>

  36.  
  37. <update id="update" parameterType="com.main.model.UserEntity" >

  38. UPDATE

  39. test.userentity

  40. SET

  41. <if test="userName != null">userName = #{userName},</if>

  42. <if test="passWord != null">passWord = #{passWord},</if>

  43. nick_name = #{nickName}

  44. WHERE

  45. id = #{id}

  46. </update>

  47.  
  48. <delete id="delete" parameterType="java.lang.Long" >

  49. DELETE FROM

  50. test.userentity

  51. WHERE

  52. id =#{id}

  53. </delete>

  54.  
  55. </mapper>

这里的配置将com.main.mapper.UserMapper中的抽象方法进行实现。

6.配置mybatis.config.xml

 
  1. <?xml version="1.0" encoding="UTF-8" ?>

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

  3. <configuration>

  4. <typeAliases>

  5. <typeAlias alias="Integer" type="java.lang.Integer" />

  6. <typeAlias alias="Long" type="java.lang.Long" />

  7. <typeAlias alias="HashMap" type="java.util.HashMap" />

  8. <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />

  9. <typeAlias alias="ArrayList" type="java.util.ArrayList" />

  10. <typeAlias alias="LinkedList" type="java.util.LinkedList" />

  11. </typeAliases>

  12. </configuration>

7.在controler中对mapper中的方法进行调用

 
  1. package com.main.controler;

  2.  
  3. import java.util.List;

  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;

  6. import org.springframework.web.bind.annotation.PathVariable;

  7. import org.springframework.web.bind.annotation.RequestMapping;

  8. import org.springframework.web.bind.annotation.RestController;

  9.  
  10. import com.main.mapper.UserMapper;

  11. import com.main.model.UserEntity;

  12.  
  13. @RestController

  14. @RequestMapping("/user")

  15. public class UserEntityControler {

  16. @Autowired

  17. private UserMapper userMapper;

  18.  
  19. @RequestMapping("/getUsers")

  20. public List<UserEntity> getUsers() {

  21. List<UserEntity> users=userMapper.getAll();

  22. return users;

  23. }

  24.  
  25. @RequestMapping("/getUser")

  26. public UserEntity getUser(Long id) {

  27. UserEntity user=userMapper.getOne(id);

  28. return user;

  29. }

  30.  
  31. @RequestMapping("/add")

  32. public void save(UserEntity user) {

  33. userMapper.insert(user);

  34. }

  35.  
  36. @RequestMapping(value="update")

  37. public void update(UserEntity user) {

  38. userMapper.update(user);

  39. }

  40.  
  41. @RequestMapping(value="/delete/{id}")

  42. public void delete(@PathVariable("id") Long id) {

  43. userMapper.delete(id);

  44. }

  45. }

8.在启动类中将mapper进行装载,否则会报错。

 
  1. package com.main;

  2.  
  3. import org.mybatis.spring.annotation.MapperScan;

  4. import org.springframework.boot.SpringApplication;

  5. import org.springframework.boot.autoconfigure.SpringBootApplication;

  6. @SpringBootApplication

  7. @MapperScan("com.main.mapper")

  8. public class SpringBootProgresqlApplication {

  9.  
  10. public static void main(String[] args) {

  11. SpringApplication.run(SpringBootProgresqlApplication.class, args);

  12. }

  13. }

接下来只需启动就可以完成spring-boot使用mybatis进行数据库操作了。

附上git地址:

https://github.com/qichangyang/spring-boot-demo

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/327195.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

CoreCLR源码探索(二) new是什么

前一篇我们看到了CoreCLR中对Object的定义&#xff0c;这一篇我们将会看CoreCLR中对new的定义和处理new对于.Net程序员们来说同样是耳熟能详的关键词&#xff0c;我们每天都会用到new&#xff0c;然而new究竟是什么&#xff1f; 因为篇幅限制和避免难度跳的太高&#xff0c;这一…

asp.net core 认证及简单集群

众所周知&#xff0c;在Asp.net WebAPI中&#xff0c;认证是通过AuthenticationFilter过滤器实现的&#xff0c;我们通常的做法是自定义AuthenticationFilter&#xff0c;实现认证逻辑&#xff0c;认证通过&#xff0c;继续管道处理&#xff0c;认证失败&#xff0c;直接返回认…

Could not open ServletContext resource [/WEB-INF/springmvc-servlet.xml]【解决方案】

第一次自学springmvc的时候&#xff0c;老是报错Could not open ServletContext resource [/WEB-INF/springmvc-servlet.xml]&#xff0c;郁闷的不要不要的。按照配置规则重新检查了一遍&#xff0c;没看出问题来&#xff0c;上网搜了一下说在web.xml里面加入: <servlet>…

成小胖学习微服务架构·基础篇

看到最近“微服务架构”这个概念这么火&#xff0c;作为一个积极上进的程序猿&#xff0c;成小胖忍不住想要学习学习。而架构师老王&#xff08;不是隔壁老王&#xff09;最近刚好在做公司基础服务的微服务化研究和落地&#xff0c;对此深有研究。 于是成小胖马上屁颠屁颠的跑过…

JDBC连接数据库教程,postgreSQL

https://blog.csdn.net/jg15617651654/article/details/63262456/ JDBC连接数据库教程&#xff0c;postgreSQL 流年你奈我何 2017-03-18 17:17:43 17389 收藏 4 分类专栏&#xff1a; Postgres 修炼之道 文章标签&#xff1a; postgresql 数据库 事务 jdbc 版权 0、概述 …

Springmvc入门案例(1)

据说&#xff0c;现在springmvc火了&#xff0c;好多企业都在使用&#xff0c;既然这样&#xff0c;咱们也得会点&#xff0c;于是乎就开始自学了&#xff0c;通过找资料&#xff0c;终于做出来了一个简单案例&#xff0c;这里分享供大家浏览&#xff0c;主要分为以下几个步骤&…

微软Project Springfield团队的F#使用心得

Project Springfield是一个用于在软件中查找关键安全错误的模糊测试服务。微软Springfield团队首席软件工程经理William Blum介绍了他们团队如何利用F#来构建云服务。 简洁性经常被认为是F#的主要优点之一。Blum提供了一些Project Springfield相关的数据&#xff1a; 为了移除一…

实现BUG自动检测 - ASP.NET Core依赖注入

我个人比较懒&#xff0c;能自动做的事绝不手动做&#xff0c;最近在用ASP.NET Core写一个项目&#xff0c;过程中会积累一些方便的工具类或框架&#xff0c;分享出来欢迎大家点评。 如果以后有时间的话&#xff0c;我打算写一个系列的【实现BUG自动检测】&#xff0c;本文将是…

玩转SpringBoot之定时任务详解

玩转SpringBoot之定时任务详解 https://www.cnblogs.com/mmzs/p/10161936.html 玩转SpringBoot之定时任务详解 阅读目录&#xff1a; 序言一、静态&#xff1a;基于注解二、动态&#xff1a;基于接口三、多线程定时任务阅读正文&#xff1a; 回到顶部 序言 使用SpringBoot创…

Java开发人员必知必会的20种常用类库和API

转载自 Java开发人员必知必会的20种常用类库和API 一个有经验的Java开发人员特征之一就是善于使用已有的轮子来造车。《Effective Java》的作者Joshua Bloch曾经说过&#xff1a;“建议使用现有的API来开发&#xff0c;而不是重复造轮子”。在本文中,我将分享一些Java开发人员应…

左耳朵耗子:不灌鸡汤,说真的年龄渐长,技术人的发展之路该怎么走

技术圈中的很多人&#xff0c;最初都坚定地认为coding能改变世界。然而三五年过去后&#xff0c;还能不忘初心的人&#xff0c;少之又少。随着年龄的增长&#xff0c;梦想已被束之高阁&#xff0c;面包慢慢占据生活的大部分。对于个人发展&#xff0c;很多成功学者会给你灌各种…

Java开发必须掌握的5种加密策略

转载自 Java开发必须掌握的5种加密策略 本文总结自《大型电商分布式系统实践——第四课》。文末给出获取全套PPT及视频的方式。 一、数字摘要 数字摘要也称为消息摘要,它是一个唯一对应一个消息或文本的固定长度的值,它由一个单向Hash函数对消息进行计算而产生。如果消息在传…

Java String格式日期加1秒(分钟或小时) java时间减一分钟,并且进行比较-时间相关的处理

https://blog.csdn.net/java0311/article/details/78047878 Java String格式日期加1秒&#xff08;分钟或小时&#xff09; chuan9966 2017-09-21 09:15:07 17101 收藏 6 文章标签&#xff1a; String格式日期加1秒 data日期加1秒 日期加1秒 版权 需求&#xff1a; 将如下…

聊下JVM内存模型

转载自 聊下JVM内存模型 1. JVM内存模型 2. 程序计数器(PC) 每个线程都会有自己私有的程序计数器(PC)。可以看作是当前线程所执行的字节码的行号指示器。 也可以理解为下一条将要执行的指令的地址或者行号。字节码解释器就是通过改变这个计数器的值来选取下一条需要执行的字节码…

泛型集合

作用 它是一个泛型类&#xff0c;而之前使用的时候并没有传递&#xff0c;说明java语法是允许的&#xff0c;这个时候传递的类型是Object类&#xff0c;虽然它是所有类的父类&#xff0c;可以存储任意的类型&#xff0c;但是在遍历、获取元素时需要原来的类型就要进行强制转换。…

ASP.NET Core + Angular 2 Template for Visual Studio

多个月以来&#xff0c;我和多个Github上的社区贡献者一起建立支持库、包&#xff0c;我们最终的目的是希望完成这样一个作为起点的模板&#xff0c;也就是基于把Typescript代码和Angular2宿主在ASP.NET Core项目中&#xff0c;这个模板包含一下这些方面&#xff1a; 服务端预加…