mysql - Spring Boot and Dao -
i have uploaded public project on github:
https://github.com/sotish/springmvc11.git
my model class is:
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 , 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; } }
i made student interface :-
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; }
}
i have database on mysql
i trying connect student database using springdrivermanager or basicdatasource.
how go it?
my main application class is:
@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 beans provided spring boot:"); }
}
my application properties file is
# 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 spring parameter 'create-drop', 'create' creates schema deleting previous data spring.jpa.hibernate.ddl-auto=create # no sql in 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 should excluded resolution #spring.thymeleaf.view-names= well, # comma-separated list of view names can resolved spring.thymeleaf.suffix=.html spring.thymeleaf.mode=html5 spring.thymeleaf.encoding=utf-8 spring.thymeleaf.content-type=text/html # ;charset=<encoding> added spring.thymeleaf.cache= true # set false hot refresh
my simple index.html user insert inputs stored on database :
<!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>
now confused following lots of tutorials don't see on springboot. don't know how go further.
please suggest me right direction learning spring. hope i've been stuck on days now.
i want make connection pool using 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(); }
how set maxactive connections, in this?
now want inject onto studentdaoimp class, this:
@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; }
i when run project spring boot app:
org.springframework.beans.factory.beancreationexception: error creating bean name 'org.springframework.boot.autoconfigure.orm.jpa.hibernatejpaautoconfiguration': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private javax.sql.datasource org.springframework.boot.autoconfigure.orm.jpa.jpabaseconfiguration.datasource; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'datasource' defined in com.sat.springmvc10application: bean instantiation via factory method failed; nested exception org.springframework.beans.beaninstantiationexception: failed instantiate [javax.sql.datasource]: factory method 'dm' threw exception; nested exception java.lang.illegalstateexception: not load jdbc driver class [jdbc.driverclassname] @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:334) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1210) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:537) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:476) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:303) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:299) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:194) @ org.springframework.beans.factory.support.constructorresolver.instantiateusingfactorymetho
could me correct mistakes.
thanks
you forgot set correct driver class name:
java.lang.illegalstateexception: not load jdbc driver class [jdbc.driverclassname]
change statement
dbs.setdriverclassname("jdbc.driverclassname");
to
dbs.setdriverclassname("com.mysql.jdbc.driver");
and should go. well, @ least exception should gone. , don't forget add driver class path.
edit:
- you have infinite recursion:
dm()
callsdm()
instead of returningdbs
. - you import wrong
@id
annotation instudent
entity (should jpa one). - you seem implement dao , use jdbc access database. when using jpa should use
entitymanager
that. , in case, when using spring data jpa (part of class path), needn't implement dao usingentitymanager
, can define jpa repository java interface methods desire.
Comments
Post a Comment