入门eclipse+springboot+Mysql+jpa操作CRUD实例

   公司项目转java开发,写了个java操作MySql例子,IDE使用的是eclipse最新版本(并且免费),java流行的框架挺多,之前用过SSH框架,最近研究下springboot框架,发现比SSH方便了许多,下面是我的操作源码及截图。

  • 项目结构

  • application类
package com.howdy;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;


@SpringBootApplication (exclude = {DataSourceAutoConfiguration.class})
@EnableAutoConfiguration
@EnableJpaRepositories
public class StartApp {

	public static void main(String[] args) {
		SpringApplication.run(StartApp.class, args);
		
	}
}
  • 实体类
package com.howdy.entity;
 
import java.util.Date;

import javax.persistence.*;


@Entity
public class T_Cod_SchoolData {

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private int id;
	public String sName;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	private String getsName() {
		return sName;
	}
	public void setsName(String sName) {
		this.sName = sName;
	}
	public int getSchIdentity() {
		return schIdentity;
	}
	public void setSchIdentity(int schIdentity) {
		this.schIdentity = schIdentity;
	}
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}
	public String getEduLevel() {
		return eduLevel;
	}
	public void setEduLevel(String eduLevel) {
		this.eduLevel = eduLevel;
	}
	public int getQqrdpm() {
		return qqrdpm;
	}
	public void setQqrdpm(int qqrdpm) {
		this.qqrdpm = qqrdpm;
	}
	public String getLbrdpm() {
		return lbrdpm;
	}
	public void setLbrdpm(String lbrdpm) {
		this.lbrdpm = lbrdpm;
	}
	public Date getCreatetime() {
		return createtime;
	}
	public void setCreatetime(Date createtime) {
		this.createtime = createtime;
	}
	private int schIdentity;
	private String province;
	private String eduLevel;
	private int qqrdpm;
	private String lbrdpm;
	private Date createtime;
}
  • dao类,实现jpa接口
package com.howdy.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.howdy.entity.T_Cod_SchoolData;

@Repository
public interface T_Cod_SchoolDao extends JpaRepository<T_Cod_SchoolData, Integer> {
	

}
  • services类
package com.howdy.services;

import java.util.List;
import java.util.Optional;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import com.howdy.dao.T_Cod_SchoolDao;
import com.howdy.entity.*;

@Service
public class T_Cod_SchoolService {

	@Resource
	private T_Cod_SchoolDao	schoolDao;
	
	/**

	* save,update ,delete 方法需要绑定事务. 使用@Transactional进行事务的绑定.

	*

	* @param User

	* 保存对象

	*/
	@Transactional
	public boolean  ExistsById(Integer id)
	{
		 return  schoolDao.existsById(id);
	}
	/**
	 * 获取列表
	 * */
	   public List<T_Cod_SchoolData> list() {
	        return schoolDao.findAll();
	    }
 	 @Transactional
 
 	public void save(T_Cod_SchoolData model) {
 		schoolDao.save(model);
 	}
	// 	//@Transactional
	// 
	// 	public T_Cod_SchoolDataModel update(T_Cod_SchoolData model)
	// {
	// 		schoolDao.(model);
	// 	}
 	@Transactional
 
 	public void delete(T_Cod_SchoolData model) {
 		schoolDao.delete(model);
 	}
}
  • 控制类
package com.howdy.api;

import java.util.Date;
import java.util.List;
import java.text.SimpleDateFormat;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonArrayFormatVisitor;
import com.howdy.entity.T_Cod_SchoolData;
import com.howdy.services.T_Cod_SchoolService;

import net.sf.json.JSONArray;

@RestController
public class CodSchoolController {

	@Autowired
	private T_Cod_SchoolService schoolService;

	@GetMapping(value = "/getId")
	public String getByName() {
		int Id;
		boolean b = schoolService.ExistsById(1);
		if (b) {
			return "The   id is exist.";
		}
		return "user id is not exist.";
	}

	@GetMapping(value = "/list")
	public String getList() {
		List<T_Cod_SchoolData> lst = schoolService.list();
		if (lst != null && lst.size() > 0) {

			JSONArray jsonArray = JSONArray.fromObject(lst);// 将取到的lst对应的(一组)值字符串转换为JSONArray

			// 将取到的cartypes对应的(一组)值字符串转换为JSONArray

			return jsonArray.toString();
		}
		return "";
	}

	@GetMapping("/save")
	public String save() {
		try {

			T_Cod_SchoolData model = new T_Cod_SchoolData();
			model.setsName("李四");
			// SimpleDateFormat sdFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			model.setProvince("上海");
			model.setCreatetime(new Date());
			schoolService.save(model);
		} catch (Exception e) {
			// TODO: handle exception
			throw e;
		} finally {
			return "save success.";
		}

	}

	@RequestMapping("/delete/{id}")

	public String delete() {
		try {
			T_Cod_SchoolData model = new T_Cod_SchoolData();
			model.setId(1);
			schoolService.delete(model);
		} catch (Exception e) {
			throw e;
		} finally {
			return "执行了删除操作!";
		}
	}

}
  • 属性配置
spring.datasource.url=jdbc:mysql://10.0.2.124:3306/spider?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=hengfeng
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
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
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
  • 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">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.howdy</groupId>
	<artifactId>sprbootdemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>sprbootdemo</name>
	<description>Demo project for Spring Boot</description>

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

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</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-data-jpa</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.hibernate</groupId>
					<artifactId>hibernate-entitymanager</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.hibernate</groupId>
					<artifactId>hibernate-core</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>5.2.10.Final</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	<!-- 连接mysql 数据库 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-cache</artifactId>
		</dependency>
		<!-- 获取json转换 -->
		<dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib</artifactId>
			<version>2.4</version>
			<classifier>jdk15</classifier>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值