gpt4 book ai didi

mysql - Spring Boot 和 Dao

转载 作者:行者123 更新时间:2023-11-29 12:09:23 25 4
gpt4 key购买 nike

我已经在 Github 上上传了一个公共(public)项目:

https://github.com/sotish/SpringMVC11.git

http://imgur.com/VC9q10C

我的模型类是:

    import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Table;

import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.data.annotation.Id;

@EntityScan
@Entity
@Table(name = "Student")
public class Student {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

@Column(nullable= false, unique=true)
private String firstName;

@Column(nullable= false, unique=true)
private String lastName;

@Column(nullable= false, unique=true)
private int age;

@Column(nullable= false)
private String email;

//getter and setters

public int getId() {
return id;
}

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

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Integer getAge() {
return age;
}

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

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

}

我制作了一个学生界面:-

package com.sat.Dao;

import java.util.List;

public interface Student {

public void insert(Student student);
public Boolean update();
public Student findById(int id);
public Student findByFirstname(String firstname);

List<Student> select(int id, String firstname, String lastname, String email, int age);
List<Student> selectAll();

}

studentDao 是

import java.util.List;

public class StudentDao implements Student{

@Override
public void insert(Student student) {
// TODO Auto-generated method stub

}

@Override
public Boolean update() {
// TODO Auto-generated method stub
return null;
}

@Override
public Student findById(int id) {
// TODO Auto-generated method stub
return null;
}

@Override
public Student findByFirstname(String firstname) {
// TODO Auto-generated method stub
return null;
}

@Override
public List<Student> select(int id, String firstname, String lastname,
String email, int age) {
// TODO Auto-generated method stub
return null;
}

@Override
public List<Student> selectAll() {
// TODO Auto-generated method stub
return null;
}

}

我在 mySql 上有一个数据库

enter image description here

我正在尝试使用 SpringDriverManager 或 BasicdataSource 连接到 Student 数据库。

如何去做?

我的主要应用程序类是:

@SpringBootApplication
@ComponentScan ({"com.sat", "com.sat.controller"} )
@Configuration
@EnableAutoConfiguration
@EnableWebMvc

public class SpringMvc10Application extends SpringBootServletInitializer{


public static void main(String[] args) {


SpringApplication application = new SpringApplication(SpringMvc10Application.class);
application.setShowBanner(false);;
application.run(args);

System.out.println("Let's inspect the beans provided by Spring Boot:");


}

}

我的应用程序属性文件是

# Spring MVC
#spring.view.prefix:classpath:/templates/
#spring.view.suffix:.html
#spring.view.view-names:jsp/*, html/*
#spring.thymeleaf.view-names:thymeleaf/*

name=Phil, david

# Server
server.port=8088

#override the spring parameter 'create-drop', 'create' creates the schema deleting the previous data
spring.jpa.hibernate.ddl-auto=create

# no sql in the log
spring.jpa.show-sql=false

# mySQL
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/student
database.username=root
database.password=root

# THYMELEAF (ThymeleafAutoConfiguration)
spring.thymeleaf.check-template-location=true
#spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.excluded-view-names= # comma-separated list of view names that should be excluded from resolution
#spring.thymeleaf.view-names= well, # comma-separated list of view names that can be resolved
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html # ;charset=<encoding> is added
spring.thymeleaf.cache= true
# set to false for hot refresh

我的简单index.html是用户插入存储在数据库中的输入的地方:

<!DOCTYPE html>
<html> <!--xmlns:th="http://www.thymeleaf.org">-->
<head>
<title>Thymeleaf tutorial: exercise 2</title>
<!--<link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" />-->
<meta charset="utf-8" />
</head>
<body>

<h1>Thymeleaf tutorial - Student Info</h1>
<h2>Student information</h2>



<input type="text" name="fname" value="" id="firstName"/>
<input type="text" name="lname" value="" id="lastName"/>

<label for="submit">submit</label><input type="submit" name="submit" value="submit" id="submit"/>

<span th:text="${name}"> </span>

</body>

</html>

现在我很困惑,因为我遵循了很多教程,但我在 springBoot 上看不到太多。我不知道如何进一步。

当我学习 Spring 时,请建议我正确的方向。我希望有人能提供帮助,因为我已经被困在这个问题上好几天了。

我想使用 spring Driver Manager 创建一个连接池:

@Bean (name = "dataSource")
public DataSource dm() {

DriverManagerDataSource dbs = new DriverManagerDataSource();

dbs.setDriverClassName("jdbc.driverClassName");
dbs.setUrl("jdbc:mysql://localhost:3306/student");
dbs.setUsername("root");
dbs.setPassword("root");

// dbs.max-active=100;


return dm();

}

如何设置 maxActive Connections,在此?

现在我想将其注入(inject)到我的 StudentDaoImp 类中,如下所示:

@Override
public List<Student> select(int id, String firstname, String lastname,
String email, int age) throws Exception {
java.sql.Connection con = ds.getConnection();

// List

con.close();

return null;
}

当我将项目作为 Spring Boot 应用程序运行时,我得到了这个:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in com.sat.SpringMvc10Application: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dm' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [jdbc.driverClassName]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMetho

谁能帮我改正错误。

谢谢

最佳答案

您忘记设置正确的驱动程序类名称:

java.lang.IllegalStateException: Could not load JDBC driver class [jdbc.driverClassName]

更改声明

dbs.setDriverClassName("jdbc.driverClassName");

dbs.setDriverClassName("com.mysql.jdbc.Driver");

你应该可以开始了。好吧,至少这个异常(exception)应该消失了。并且不要忘记将驱动程序添加到您的类路径中。

编辑:

  • 您有一个无限递归:dm() 调用 dm() 而不是简单地返回 dbs
  • 您在 Student 实体中导入了错误的 @Id 注释(应该是 JPA 注释)。
  • 您似乎实现了 DAO 并使用 JDBC 来访问数据库。使用 JPA 时,您应该使用 EntityManager 来执行此操作。在这种情况下,当使用 Spring Data JPA(类路径的一部分)时,您甚至不需要使用 EntityManager 实现 DAO,您可以简单地使用方法定义 JPA 存储库 Java 接口(interface)你想要的。

关于mysql - Spring Boot 和 Dao,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30948619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com