springboot简单之spring-boot-starter-data-jpa(二)

3 篇文章 0 订阅
3 篇文章 0 订阅

一、目录

  对于static里的easyui就到官网下了

  1.数据库表user.sql文件

/*
 Navicat Premium Data Transfer

 Source Server         : vmroot
 Source Server Type    : MySQL
 Source Server Version : 80013
 Source Host           : 127.0.0.1:3306
 Source Schema         : springboot

 Target Server Type    : MySQL
 Target Server Version : 80013
 File Encoding         : 65001

 Date: 21/04/2019 18:35:24
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户名',
  `password` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密码',
  `name` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '姓名',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, 'zhangsan', '123456', '张三');
INSERT INTO `user` VALUES (2, 'lisi', '123456', '李四');
INSERT INTO `user` VALUES (3, 'wangwu', '123456', '王五');
INSERT INTO `user` VALUES (4, 'zhangwei', '123456', '张伟');
INSERT INTO `user` VALUES (5, 'lina', '123456', '李娜');
INSERT INTO `user` VALUES (6, 'lilei', '123456', '李磊');

SET FOREIGN_KEY_CHECKS = 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.lcx</groupId>
  <artifactId>springboot-jpa</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
    <!-- Inherit defaults from Spring Boot -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
	</parent>
  
  	<!-- Add typical dependencies for a web application -->
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		
	</dependencies>
  
</project>

  3.application.properties配置文件,jpa的配置要配置hibernate的东西

#DB 
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot
spring.datasource.username=root
spring.datasource.password=root

#jpa
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy

  4.实体类user.java

package cn.lcx.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
 * 用户实体类
 * @author kuxin
 *
 */
@Entity
@Table(name="user")
public class User implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8703643314757223600L;

	@Id
	@Column(name="id")
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Long id;
	
	@Column(name="user_name")
	private String username;
	
	@Column(name="password")
	private String password;
	
	@Column(name="name")
	private String name;
	
	public Long getId() {
		return id;
	}
	public void setId(Long 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;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + ", name=" + name + "]";
	}
	
	
	
}

  5.IUserDao接口继承jpa,这个JpaRepository里面有基本的增删改查方法

package cn.lcx.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import cn.lcx.domain.User;

/**
 * 	用户的持久层接口
 * @author kuxin
 * 	继承jpa接口
 */
@Repository("userDao")
public interface IUserDao extends JpaRepository<User, Long> {

	/**
	 * 查询所有用户
	 * @return
	 
	List<User> findAll();
	*/
}

  6.用户的业务层实现类UserServiceImpl

package cn.lcx.service.impl;

import java.util.List;

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

import cn.lcx.dao.IUserDao;
import cn.lcx.domain.User;
import cn.lcx.service.IUserService;
/**
 * 用户的业务层实现类
 * @author kuxin
 *
 */
@Service("userService")
public class UserServiceImpl implements IUserService {
	
	@Autowired
	private IUserDao userDao;
	
	@Override
	public List<User> findAllUser() {
		return userDao.findAll();
	}

}

7. 用户的业务层接口IUserService

package cn.lcx.service;

import java.util.List;

import cn.lcx.domain.User;

/**
 * 用户的业务层接口
 * @author kuxin
 *
 */
public interface IUserService {

	/**
	 * 查询所有用户
	 * @return
	 */
	List<User> findAllUser();
}

8.用户的控制层UserController

package cn.lcx.web.controller;

import java.util.List;

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

import cn.lcx.domain.User;
import cn.lcx.service.IUserService;

/**
 * 用户的控制层
 * @author kuxin
 *
 */
@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	private IUserService userService;
	
	@RequestMapping("/findAll")
	public List<User> findAllUser(){
		List<User> users = userService.findAllUser();
		return users;
	}
	
}

9.引导类Application

package cn.lcx;

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

/**
 * spring boot 的引导类
 * @author kuxin
 *
 */
@SpringBootApplication
public class Application {

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

10.user.html页面

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>人员信息</title>
    <link rel="stylesheet" type="text/css" href="easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="easyui/themes/icon.css">
    <script type="text/javascript" src="easyui/jquery.min.js"></script>
    <script type="text/javascript" src="easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="easyui/locale/easyui-lang-zh_CN.js"></script>
</head>
<body>
    <h2>人员信息</h2>
    <div style="margin:20px 0;"></div>
    <table class="easyui-datagrid" title="人员信息" style="width:500px;height:250px"
            data-options="singleSelect:true,collapsible:true,url:'/user/findAll',method:'get'">
        <thead>
            <tr>
                <th data-options="field:'id',width:80">ID</th>
                <th data-options="field:'username',width:100">username</th>
                <th data-options="field:'password',width:80,align:'right'">password</th>
                <th data-options="field:'name',width:60,align:'center'">name</th>
            </tr>
        </thead>
    </table>
 
</body>
</html>

二、启动效果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值