您的位置:首页 > 编程语言 > Java开发

[置顶] springboot整合mybatis方式一

2017-04-26 17:54 507 查看
简介 

   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:mysq
9563
l://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来配置,很有亮点,希望指正,如有雷同,请谅解
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: