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

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

  • 项目结构

入门eclipse+springboot+Mysql+jpa操作CRUD实例_第1张图片

  • 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 {
	

}
  • 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 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 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配置


	4.0.0

	com.howdy
	sprbootdemo
	0.0.1-SNAPSHOT
	jar

	sprbootdemo
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.4.RELEASE
		
	

	
		UTF-8
		UTF-8
		1.8
	
	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.boot
			spring-boot-starter-data-jpa
			
				
					org.hibernate
					hibernate-entitymanager
				
				
					org.hibernate
					hibernate-core
				
			
		
		
			org.hibernate
			hibernate-core
			5.2.10.Final
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	
		
			mysql
			mysql-connector-java
		

		
			org.springframework.boot
			spring-boot-starter-cache
		
		
		
			net.sf.json-lib
			json-lib
			2.4
			jdk15
		
	
	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



  • 源码下载地址https://download.csdn.net/download/haojuntu/10606158

你可能感兴趣的:(java)