sts中Mysql的连接和一些简单的操作

1:Mysql数据库的连接

1.1 在sts中连接Mysql数据库是在application中写jdbc的连接

spring:
  profiles:
    active:
      - dev
  datasource :
   driver-class-name: com.mysql.jdbc.Driver
   url: jdbc:mysql://localhost:3306/student
   username: root
   password: 123456
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

1.2对数据库进行增删查改

1.2.1:现在Model层里定义用户

package com.cy.coo.li;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Man {

    @Id
    @GeneratedValue
    private Integer id;
    private Integer age;
    private String cupSize;

    public Man(){

    }

    public Integer getId() {
        return id;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getCupSize() {
        return cupSize;
    }

    public void setCupSize(String cupSize) {
        this.cupSize = cupSize;
    }

}

1.2.2:运用了一个JpaRepository<>的接口来实现对命名的规范

package com.cy.coo.li;

import java.util.List;

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

public interface manInterface extends JpaRepository<Man, Integer> {
   //通过年龄查询,因为查出来很可能有很多个,所以使用集合
     public List<Man> findByAge(Integer age);
}

1.2.3:在Dao层实现对数据库的增删查改

package com.cy.coo.li;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ManContent {

    @Autowired
    private manInterface manl;

    @Autowired
    private manService mans;

    /*
     * 查询所有人的数据
     */
    @GetMapping(value = "/man")
    /*
     * List指的是集合.<>是泛型,里面指定了这个集合中存放的是什么数据. List<Man>代表把Man类中的信息的对象都在里面了
     */
    public List<Man> manlist() {
        return manl.findAll();
    }

    /*
     * 添加
     */
    @PostMapping(value = "/man")
    // @RequestParam获取参数
    /*
     * 因为save返回的时添加进去的对象,所以返回类型就是这个对象
     */
    public Man manAdd(@RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age) {
        Man man = new Man();

        man.setCupSize(cupSize);
        man.setAge(age);
        return manl.save(man);
    }

    // 查询
    @GetMapping(value = "/man/{id}")
    //因为
    public Man manFindOne(@PathVariable("id") Integer id) {
        System.out.println(id);
        return manl.findOne(id);

    }
       //更新
    @PutMapping(value="/man{id}")
    public Man manUpdata(@RequestParam(value = "id") Integer id,
    @RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age){
        Man manll=new Man();
        manll.setId(id);
        manll.setAge(age);
        manll.setCupSize(cupSize);
        return manl.save(manll);
    }
    //删除
    @DeleteMapping(value="/man{id}")
    public void manDelete(@RequestParam("id") Integer id){
        //因为delete的返回值为null所以就是没得1返回值
        manl.delete(id);
    }
    //通过年龄查询
     @GetMapping(value="/man/age/{age}")
     public List<Man> manListAge(@PathVariable("age") Integer age){
         return manl.findByAge(age);
     }

     @PostMapping(value="/man/two")
     public void manTwo(){
         mans.InsertTwo();
     }

}

1.2.4service层里实现业务

package com.cy.coo.li;

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

@Service
public class manService {

    @Autowired
    private manInterface manl;

    public void InsertTwo(){
        Man manA=new Man();
        manA.setAge(21);
        manA.setCupSize("F");
        manl.save(manA);

        Man manB=new Man();
        manB.setAge(21);
        manB.setCupSize("F");
        manl.save(manB);
    }
}
时间: 2024-08-03 06:58:25

sts中Mysql的连接和一些简单的操作的相关文章

golang中mysql建立连接超时时间timeout 测试

本文测试连接mysql的超时时间. 这里的"连接"是建立连接的意思. 连接mysql的超时时间是通过参数timeout设置的. 1.建立连接超时测试 下面例子中,设置连接超时时间为5s,读超时时间6s. MySQL server IP是192.168.0.101,端口3306. 每3s执行一次SQL. // simple.go package main import ( "database/sql" "log" "time"

virtualbox虚拟机中mysql远程连接登陆报2003错误的解决方法

最近在virtualbox中安装了Ubuntu 14,配置了一个mysql server,设置的桥接网络模式.在其他电脑连接的时候,总是报2003错误.开始以为是localhost没有置换为%,运行update语句将其置换,依然不行.后来发现是因为mysql的默认配置文件/etc/mysql/my.cnf 中有一行: bind-address            = 127.0.0.1 就是说,默认绑定了ip,只限本机访问.将本行加#注释掉,远程访问正常.

PHP 中Mysql配置连接的问题

装Mysql就不说了,网上教程一把,一般都行 http://tech.163.com/06/0206/11/299AMBLT0009159K_3.html 这个就挺好的. 配置好(主要是 修改php.ini文件,启动一些扩展,把 extension=php_gd2.dll extension=php_mysql.dll extension=php_mysqli.dll extension=php_openssl.dll 这里要注意看清楚这几个都放开 ) <?php $link=mysql_con

30.1. MySQL数据库简介,客户端简单操作,安装

文件管理系统的缺点 编写应用程序不方便数据冗余不可避免应用程序依赖性不支持对文件的并发访问数据间联系弱难以按用户视图表示数据无安全控制功能 数据库管理系统的优点 相互关联的数据的集合较少的数据冗余程序与数据相互独立保证数据的安全.可靠最大限度地保证数据的正确性数据可以并发使用并能同时保证一致性 数据库管理系统 数据库是数据的汇集,它以一定的组织形式存于存储介质上DBMS是管理数据库的系统软件,它实现数据库系统的各种功能.是数据库系统的核心DBA(团队):负责数据库的规划.设计.协调.维护和管理等

PowerBuilder中使用JDBC连接MYSQL 无法使用COUNT(*) MAX() 函数的处理

PowerBuilder中使用JDBC连接MYSQL 遇到的错误: long ll_count // Profile mysql SQLCA.DBMS = "JDBC" SQLCA.LogPass = "123" SQLCA.LogId = "root" SQLCA.AutoCommit = False SQLCA.DBParm = "Driver='com.mysql.jdbc.Driver',URL='jdbc:mysql://lo

sts从mysql数据库中反向生成实体类

首先我们要在sts中建立mysql的数据库连接 1. 当点击ok之后,如果没有报错的话就应该是建立好了,我们可以点击查看这个数据库中所有的表 我们就可以再sts进行数据库操作了,具体如下: 点击如下按钮打开一个或者右键点击 open sql scrapbook 确定数据连接好之后,我们要去Hibernate的网站下载这个工具包.一个大约14M的ZIP压缩文件. http://jaist.dl.sourceforge.net/sourceforge/jboss/HibernateTools-3.2

虚拟机中MySQL连接问题:Lost connection to MySQL server at &#39;reading initial communication packet, system error: 0 以及 host is not allowed to connect mysql

环境:在VirtualBox中安装了Ubuntu虚拟机,网络使用了NAT模式,开启了端口转发. 局域网内其他计算机访问虚拟机中的MySQL Server出现两个问题: Lost connection to MySQL server at 'reading initial communication packet, system error: 0 以及 host is not allowed to connect mysql 1.解决Lost connection to MySQL server

【转】mysql数据库中实现内连接、左连接、右连接

[转]mysql数据库中实现内连接.左连接.右连接 内连接:把两个表中数据对应的数据查出来 外连接:以某个表为基础把对应数据查出来 首先创建数据库中的表,数据库代码如下: /* Navicat MySQL Data Transfer Source Server : localhost_3306 Source Server Version : 50150 Source Host : localhost:3306 Source Database : store Target Server Type

Linux学习笔记之——ubuntu中mysql允许远程连接

摘要:一般mysql默认安装出于安全考虑.一般只会让主机连接到mysql.而其他的机器通过远程的方式是连接不上mysql的.这样想在别的机器上远程操作主机的mysql就会denied.当然备份也会被拒绝.记录一下如何解决mysql支持远程. 一:简介 环境依然是前面的环境.可以在其他机器上测试一下是否能远程连接本主机的mysql.我主机的IP是192.168.26.200.mysql用户是root.密码是password.键入如下命令.并输入密码: mysql–h192.168.26.200 –