关闭

SpringBoot整合MyBatis

标签: mybatisSpringBootSpringBoot整合MyBatis
1126人阅读 评论(0) 收藏 举报
分类:

项目结构如图所示


创建一个Maven项目


创建application.properties

######################################
###spring datasource
######################################
spring.datasource.url=jdbc:mysql://localhost:3306/text
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

修改pom.xml(注意各个标签的位置)

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.chaihuo</groupId>
    <artifactId>SpringBootMyBatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!--继承  -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
         <!-- 设置字符集 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!-- 设置java版本 -->
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- Spring Boot  -->
        <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>
        </dependency>


        <!--热部署.jar -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <!-- <version>1.5.4.RELEASE</version> -->
        </dependency>




        <!-- mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!-- MyBatis-SpringBoot -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>



    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- 热部署的的配置,只有配置了此属性,才能restart -->
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Update项目(项目名称前面的X会消失)


创建实体类

package com.imooc.entity;

public class Student {

	private Integer id;
	private String name;
	private Integer age;
	private Integer score;

	public Student() {
	}

	public Student(String name, Integer age, Integer score) {
		this.name = name;
		this.age = age;
		this.score = score;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public Integer getScore() {
		return score;
	}

	public void setScore(Integer score) {
		this.score = score;
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + ", score=" + score + "]";
	}

}


创建基于注解的Mapper

package com.imooc.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.imooc.entity.Student;

@Mapper
public interface StudentMapper {

	@Insert("insert into student (name,age,score) values(#{name},#{age},#{score})")
	public void addStudent(Student stu);

	@Delete("delete from student where id =#{id}")
	public void deleteById(int id);

	@Update("update student set name=#{name},age=#{age},score=#{score} where id=#{id}")
	public void update(Student stu);

	@Select("select id,name,age,score from student where id = #{id}")
	public Student selectById(int id);

}


测试

package com.imooc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import com.imooc.entity.Student;
import com.imooc.mapper.StudentMapper;

@SpringBootApplication
public class App {

	public static void main(String[] args) {
		ConfigurableApplicationContext context=SpringApplication.run(App.class, args);
		StudentMapper studentDao=context.getBean(StudentMapper.class);
		Student stu=studentDao.selectById(1);
		System.out.println(stu);
		context.close();

	}

}


注意:

application.properties的名称不能错,位置不能错

项目的入口类(此项目中是App.),所有要扫描的包都必须在App的同包或者子包下,否则扫描不到



1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:33777次
    • 积分:734
    • 等级:
    • 排名:千里之外
    • 原创:38篇
    • 转载:0篇
    • 译文:1篇
    • 评论:5条
    文章存档
    最新评论