Reputation: 84
I am new with spring and now I am accessing to a database and showing the results in a jsp. This is the xml file I use to connect with the database:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://192.168.1.11:3306/databasename" />
<property name="username" value="****" />
<property name="password" value="****" />
</bean>
<bean id="Am_facultyentityDAO" class="com.bdAlmamater.model.Am_facultyentityJdbcDAO">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="Am_campusentityDAO" class="com.bdAlmamater.model.Am_campusentityJdbcDAO">
<property name="dataSource" ref="dataSource" />
</bean>
It all works fine, but I have a lot of tables in my database and I would like to know if I have to put for each table something like this:
<bean id="Am_campusentityDAO" class="com.bdAlmamater.model.Am_campusentityJdbcDAO">
<property name="dataSource" ref="dataSource" />
</bean>
or is it a shorter way to write it? Thank you in advance.
Upvotes: 1
Views: 522
Reputation: 396
You should think of using Hibernate/JPA and/or Spring Data JPA.
This way you won't have to write deep DAOs but just plain simple interfaces with a conventional naming with maybe some custom queries.
The next two links will help you:
Or if you like to use Spring Context XML to have an application blueprint, you could just use:
<bean id="Am_campusentityDAO" class="com.bdAlmamater.model.Am_campusentityJdbcDAO"/>
and autowire the DataSource field:
@Autowired
Datasource datasource;
Upvotes: 2
Reputation: 20155
You don't need to declare your classes in bean configuration file everytime, you could use Spring's stereo types (like Component, Service, Repository and Controller) to identify your classes.
In order to use this stereotypes,
You need to first specify
<content:componet-scan base-package="com.bdAlmamater.model" >
and specify packages to scan (i.e packages where your stereo type annotated class exists )
If you are usign the lower version of spring (i.e 2.x), you need to specify
<context:annotation-config>
To let the spring identify annotations, if you are using Higher versions (3.x, 4.x) of spring, you don't need to specify it.
and then annotate your classes with @Repository stereotype (Because you are usign it on Dao layer)
i.e
@Repository
class Am_campusentityJdbcDAO
if it need any dependencies, you just need to inject them using @Autowired
annotation
in your case
@Autowired
Datasource datasource;
Upvotes: 1