springBoot入门配置和使用

创建一个mevan项目,在pom.xml文件配置jar包

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.2.RELEASE</version>
	</parent>

再导入web的jar包,因为springBoot里有依赖web,所以在这里导入web的jar包不需要版本号

	<dependencies>
		 <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
    </dependencies>

因为我接下来要连接数据库,所以在导入个自动生成sql语句的jar包

	<!-- 自动生成sql语句配置 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

jar包引入完成,接下来配置MySQL的四要素


创建一个application.properties文件,并配上MySQL四要素

spring.datasource.url=jdbc:mysql://localhost/hcc
spring.datasource.username=root
spring.datasource.password=sa
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

接下来创建一个启动springBoot的类TestMail.java



package com.et.sb;

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

//从源代码中得知 @SpringBootApplication 被 @Configuration、@EnableAutoConfiguration、@ComponentScan 注解所修饰,换言之 Springboot 提供了统一的注解来替代以上三个注解
@SpringBootApplication

public class TestMail {

	public static void main(String[] args) {
		SpringApplication.run(TestMail.class, args);
	}
}
在controller包里创建一个userController.java类

package com.et.sb.contorller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.et.sb.model.User;
import com.et.sb.service.UserService;
//代替了@ResponseBody和@controller注解

@RestController
public class UserContorller {
	//这个注解就是spring可以自动帮你把bean里面引用的对象的setter/getter方法省略,它会自动帮你set/get。
	@Autowired
	UserService userService;

	@RequestMapping("/query")
	public Iterable<User> queryUserAll() {
		Iterable<User> userList = userService.queryUserAll();
		return userList;
	}

	@RequestMapping("/")
	public String query() {
		return "hello";
	}
}

在model包里创建一个user.java类

package com.et.sb.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
//指名这是一个实体Bean
@Entity
//连接MySQL表,因表名和类名不一致所以加上(name = "user_2")
@Table(name = "user_2")
public class User {
	//表主键
	@Id
	private int id;

	//表列名,nullable=false 不能为空,name是因为属性名和列名不一致所以加上
	@Column(nullable = false, name = "u_name")
	private String name;
	
	//表列名,nullable=false 不能为空,name是因为属性名和列名不一致所以加上
	@Column(nullable = false, name = "psw")
	private String password;

	public int getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

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

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}

从上面代码来看开发实体Bean非常简单,比起普通的JavaBean就是多了些注释。@Entity注释指名这是一个实体Bean,@Table注释指定了Entity所要映射带数据库表,其中@Table.name()用来指定映射表的表名。如果缺省@Table注释,系统默认采用类名作为映射表的表名。实体Bean的每个实例代表数据表中的一行数据,行中的一列对应实例中的一个属性。

@Column注释定义了将成员属性映射到关系表中的哪一列和该列的结构信息,属性如下:

1)name:映射的列名。如:映射tbl_user表的name列,可以在name属性的上面或getName方法上面加入;

2)unique:是否唯一;

3)nullable:是否允许为空;

4)length:对于字符型列,length属性指定列的最大字符长度;

5)insertable:是否允许插入;

6)updatetable:是否允许更新;

7)columnDefinition:定义建表时创建此列的DDL;

8)secondaryTable:从表名。如果此列不建在主表上(默认是主表),该属性定义该列所在从表的名字。

@Id注释指定表的主键,它可以有多种生成方式:

1)TABLE:容器指定用底层的数据表确保唯一;

2)SEQUENCE:使用数据库德SEQUENCE列莱保证唯一(Oracle数据库通过序列来生成唯一ID);

3)IDENTITY:使用数据库的IDENTITY列莱保证唯一;

4)AUTO:由容器挑选一个合适的方式来保证唯一;

5)NONE:容器不负责主键的生成,由程序来完成

在service包下创建一个userService接口

package com.et.sb.service;

import com.et.sb.model.User;

public interface UserService {

	Iterable<User> queryUserAll();
}

在service.impl包下创建一个userServiceImpl.java类并实现userService接口。

package com.et.sb.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.et.sb.dao.UserDao;
import com.et.sb.model.User;
import com.et.sb.service.UserService;

//@Service服务层组件,用于标注业务层组件,表示定义一个bean,自动根据bean的类名实例化一个首写字母为小写的bean,
@Service
public class UserServiceImpl implements UserService {
	// 这个注解就是spring可以自动帮你把bean里面引用的对象的setter/getter方法省略,它会自动帮你set/get。
	@Autowired
	UserDao userdao;

	@Override
	public Iterable<User> queryUserAll() {
		// TODO Auto-generated method stub
		Iterable<User> userList = userdao.findAll();
		return userList;
	}

}

在dao包里创建一个userDao接口,并继承CrudRepository<User, Serializable>

package com.et.sb.dao;

import java.io.Serializable;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.et.sb.model.User;
//注解是告诉Spring,让Spring创建一个名字叫“userDao”的UserDaoImpl实例。
@Repository
public interface UserDao extends CrudRepository<User, Serializable> {

}

所有类和配置都已完成,接下来运行TestMail.java类


然后在浏览器输入localhost:8080/query即可


这样一个简单的SpringBoot就完成了!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值