springboot+mybatis

15 篇文章 0 订阅
13 篇文章 0 订阅

 

 

 

项目结构:

项目名称:springboot-mybatis

注:

启动时遇到这样的错误

a. Exception in thread "main" java.lang.UnsupportedClassVersionError: org/springframework/boot/SpringApplication : Unsupported major.minor version 52.0

原因:52.0--java1.8

解决方案:1.更换jdk1.8

                   2.将spring-boot-starter-parent改为1.4.1.RELEASE版本

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.1.RELEASE</version>
    <relativePath/> 
</parent>
b.当使用jdk是1.7时

    1.创建完项目后修改

<properties>
    <java.version>11</java.version>
</properties>

<properties>
    <java.version>1.7</java.version>
</properties>
  2.Settings--->Build,Execution,Deployment--->Compiler--->Java Compiler

将11改为1.7

 

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springboot.mybatis</groupId>
    <artifactId>springboot-mybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-mybatis</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.7</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--mybatis start-->
        <!--mybatis整合springboot的起步依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <!--mysql的jdbc驱动包-->
        <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
        <!--mybatis end-->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <!-- IDEA-解决: org.apache.ibatis.binding.BindingException:Invalid bound statement (not found) -->
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

</project>

application.properties

spring.datasource.url=jdbc:mysql://*.*.*.*:3306/test?useUnicode=true&connectTimeout=3000&socketTimeout=600000&characterEncoding=UTF-8&useSSL=false
spring.datasource.username=coredata
spring.datasource.password=coredata123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.type-aliases-package=com.example.until
mybatis.mapper-locations=classpath:mapping/*.xml

注:

mybatis mapper-locations作用

application上配置了@MapperScan(扫面mapper类的路径)和pom.xml中放行了mapper.xml后,配置mapper-locations没有意义

查找后得知,如果mapper类和mapper.xml不在同一个路径下时,mapper-locations就有用了

 

访问:

http://localhost:8080/boot/select

http://localhost:8080/boot/delete

http://localhost:8080/boot/update

http://localhost:8080/boot/insert

StudentController.java

package com.example.controller;

import com.example.service.StudentService;
import com.example.until.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class StudentController {
    @Autowired
    private StudentService studentService;

    //查询
    @RequestMapping("/boot/select")
    public @ResponseBody String  GetUser(){

        return studentService.Sel(3).toString();
    }

    //删除
    @RequestMapping("/boot/delete")
    public @ResponseBody String  deleteUser(){

        return studentService.deleteByUserId(2)+":删除";
    }

    //修改
    @RequestMapping("/boot/update")
    public @ResponseBody String  updateUser(){
        Student stu=new Student(1,"xiaohong","12222");
        return studentService.updateByPrimaryKeySelective(stu)+":修改";
    }

    //添加
    @RequestMapping("/boot/insert")
    public @ResponseBody String  insertUser(){
        Student stu=new Student(5,"ceshi","135781");
        return studentService.insertStudent(stu)+":添加";
    }


}

StudentService.java

package com.example.service;

import com.example.until.Student;
import com.example.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentService {

    @Autowired
    StudentMapper studentMapper;

    /**
     * 查询
     * @param userId
     * @return
     */
    public Student Sel(int userId){
        return studentMapper.Sel(userId);
    }

    /**
     * 删除
     * @param userId
     * @return
     */
    public int deleteByUserId(int userId){
        return studentMapper.deleteByUserId(userId);
    }

    /**
     * 添加
     * @param student
     * @return
     */
    public int insertStudent(Student student){
        return studentMapper.insertStudent(student);
    }

    /**
     * 修改
     * @param student
     * @return
     */
    public int updateByPrimaryKeySelective(Student student){
        return studentMapper.updateByPrimaryKeySelective(student);
    }

}

StudentMapper.java

注:在mybatis的Mapper中添加@Mapper注解或者在运行的主类上添加@MapperScan("com.example.mapper")

package com.example.mapper;


import com.example.until.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;

@Mapper
/*@Component*/
public interface StudentMapper {
    /**
     * 查询
     * @param userId
     * @return
     */
    Student Sel(int userId);

    /**
     * 删除
     * @param userId
     * @return
     */
    int  deleteByUserId(Integer userId);

    /**
     * 修改
     * @param stu
     * @return
     */
    int  updateByPrimaryKeySelective(Student stu);

    /**
     * 添加
     * @param stu
     * @return
     */
    int insertStudent(Student stu);
}

Student.java

package com.example.until;

import org.springframework.boot.autoconfigure.domain.EntityScan;


public class Student {
    private int userId;
    private String userName;
    private String userPwd;

    public Student() {

    }

    public Student(int userId, String userName, String userPwd) {

        this.userId = userId;
        this.userName = userName;
        this.userPwd = userPwd;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPwd() {
        return userPwd;
    }

    public void setUserPwd(String userPwd) {
        this.userPwd = userPwd;
    }

    @Override
    public String toString() {
        return "Student{" +
                "userId=" + userId +
                ", userName='" + userName + '\'' +
                ", userPwd='" + userPwd + '\'' +
                '}';
    }
}

SpringbootMybatisApplication.java

package com.example;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;



@SpringBootApplication
public class SpringbootMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisApplication.class, args);
    }

}

StudentMapper.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.example.mapper.StudentMapper">

    <resultMap id="BaseResultMap" type="com.example.until.Student">
        <result column="userId" jdbcType="INTEGER" property="userId" />
        <result column="userName" jdbcType="VARCHAR" property="userName" />
        <result column="userPwd" jdbcType="VARCHAR" property="userPwd" />
    </resultMap>
    <sql id="Base_Column_List" >
      userId,
      userName,
      userPwd
  </sql>

    <select id="Sel" resultType="com.example.until.Student">
        select  <include refid="Base_Column_List" />  from studentAndspringbootdemo where userId = #{userId}
    </select>
    <insert id="insertStudent" parameterType="com.example.until.Student">
        insert into studentAndspringbootdemo (userId, userName, userPwd) values (
        #{userId},
        #{userName},
        #{userPwd}
        )
    </insert>
    <delete id="deleteByUserId" parameterType="java.lang.Integer" >
      delete from studentAndspringbootdemo
      where userId = #{userId}
  </delete>
    <update id="updateByPrimaryKeySelective" parameterType="com.example.until.Student" >
        update studentAndspringbootdemo
        <set >
            <if test="userId != null" >
                userId = #{userId},
            </if>
            <if test="userName != null" >
                userName = #{userName},
            </if>
            <if test="userPwd != null" >
                userPwd = #{userPwd}
            </if>
        </set>
        where userId = #{userId}
    </update>
</mapper>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值