springboot整合mybatis方式一

本文介绍了如何将Spring Boot与Mybatis进行整合。通过在pom.xml中添加必要的依赖,如spring-boot-starter、spring-boot-starter-web、mysql-connector-java和mybatis-spring-boot-starter,以及配置application.properties文件来设置数据库连接和日志级别,实现Spring Boot项目与Mybatis的集成。同时,文章提到了Spring Boot的一些特性,如内嵌Tomcat、自动配置和简化Maven配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简介 

   Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者的。

特性

1.嵌入tomcat,无需打成war包(可以与docer容器配合)

2绝对令配置和无需xml文件(官方说法)

3快速启动,注解完全化

4简化maven配置

实战之配置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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>JIXING</artifactId>
        <groupId>com.jixing</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>jixing-consumer-xuanke</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId><!-- 配置快速启动 -->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId><!--整合web (springmvc)  -->
        </dependency>
        <!-- MYSQL依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId><!--mysql驱动包  -->
        </dependency>
        <!-- mybatis依赖 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId><!--boot整合mybatis的关键包annotation  -->
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
实战之application.properties
#配置端口号默认为8080
server.port=7903
#配置dao层的log输出(控制台打印出sql语句及参数、结果信息,推荐配置,红色为路径)
logging.level.com.xuanke.dao= trace
#配置web的log日志
logging.level.org.springframework.web = info
#debug出项目信息
logging.level.com.xuanke=debug
#JPA configure mysql路径,用户名、密码 以及驱动类
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = admin
spring.datasource.driverClassName = com.mysql.jdbc.Driver
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# DDL mode. This is actually a shortcut for the "hibernate.hbm2ddl.auto" property. Default to "create-drop" when using an embedded database, "none" otherwise.
spring.jpa.hibernate.ddl-auto = update
# Hibernate 4 naming strategy fully qualified name. Not supported with Hibernate 5.
spring.jpa.hibernate.naming.strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

实战之实体类User.java

package com.xuanke.entity;

public class User{
	private int id;
	
	private String username;
	private String password;

	public int getId() {
		return id;
	}

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

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}
实战之DAO层UserDao.java
public interface UserDao {
    @Select("select * from sec_user where id=#{id}")//mybatis的注解
    public User findUser(@Param("id") int id);
}
实战之启动类
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.xuanke.dao.UserDao;
import com.xuanke.entity.User;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@RestController
@MapperScan("com.xuanke.dao")
public class XuankeApplication {
	@Autowired
	private UserDao userDao;
	@Autowired
	private EurekaClient eurekaClient;

	@RequestMapping("/hello/{id}")
	@ResponseBody
	public User hello(@PathVariable("id") int id){
		User userInfo = userDao.findUser(1);
		return userInfo;
	}
	/*@GetMapping("/eureka-instance")
	public String serviceUrl(){
		InstanceInfo instanceInfo = this.eurekaClient.getNextServerFromEureka("localhost", false);
		return  instanceInfo.getHomePageUrl();
	}*/

	public static void main(String[] args) {
		SpringApplication.run(XuankeApplication.class, args);
	}
}
最后直接运行启动类,然后在浏览器直接访问localhost:7903/hello/1
自行配置数据库(数据库名:msm,表名:sec_user 字段: int id   varchar username varchar password),不怕喷,下一个将用xml来配置,很有亮点,希望指正,如有雷同,请谅解






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值