springboot与mybatis整合

完整代码如下:

(1)目录结构






















(2)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>cn.zw</groupId>
  <artifactId>springboot-20170105</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.3.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.version>1.7</java.version>
	</properties>
	
	<dependencies>
		<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>
		
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.1.1</version>
		</dependency>
		
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.21</version>
		</dependency>
	</dependencies>
  
  <build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
		<!-- 如果是通过parent方式继承spring-boot-starter-parent则不用此插件 -->
	</build>
  
</project>

(3)启动类Start,注意启动类在最底层

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

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

(4)controller 控制层

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.zw.custDao.CustMapper;
import cn.zw.custEntity.Cust;

@Controller
@RequestMapping("/cust")
public class CustController {
	@Resource
	CustMapper custMapper;
	
	@RequestMapping("/list")
	@ResponseBody
	public Cust getCustList(String phone){
		Cust c=new Cust();
		c=custMapper.getCust(phone);
		System.out.println(c);
		return c;
	}
}

(5)Dao层,一个@Mapper注解就搞定


import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import cn.zw.custEntity.Cust;

@Mapper
public interface CustMapper {
	
	@Select("select bc.id as id,bc.cust_name as name,bc.cust_mobile as phone,bc.cust_ic as ic,bc.bank_account as bank"
			+ " from bg_cust_info bc where bc.cust_mobile=#{phone};")
	Cust getCust(String phone);
}


(6)实体类

public class Cust implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -6808147551715637272L;
	private String id;
	private String name;
	private String ic;
	private String phone;
	private String bank;
	public Cust() {
		
	}
	
	public Cust(String id, String name, String ic, String phone, String bank) {
		super();
		this.id = id;
		this.name = name;
		this.ic = ic;
		this.phone = phone;
		this.bank = bank;
	}

	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public String getIc() {
		return ic;
	}
	public void setIc(String ic) {
		this.ic = ic;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getBank() {
		return bank;
	}
	public void setBank(String bank) {
		this.bank = bank;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Cust other = (Cust) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Cust [id=" + id + ", name=" + name + ", ic=" + ic + ", phone=" + phone + ", bank=" + bank + "]";
	}
	
	
}


(7)application.properties编写   

spring.datasource.url=jdbc:mysql://localhost:3306/自己数据库名
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值