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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值