本來(lái)準(zhǔn)備直接寫(xiě)srping+hibernate的集成了,不過(guò)為了讓自己鞏固知識(shí),為了讓看到文章的更了解流程,所以把最開(kāi)始搭建的流程走下。
上篇講到了springmvc實(shí)現(xiàn)了控制層的helloworld實(shí)例,這篇接著寫(xiě)service層,和dao層的整合。
這里會(huì)需要幾個(gè)配置文件,applicationContext.xml,test-service.xml,test-dao.xml
applicationContext主要就是包含后面兩個(gè)文件了。其實(shí)這三個(gè)文件可以寫(xiě)在一個(gè)配置文件中,不過(guò)為了更好的分離,所以拆開(kāi)了dao的配置文件,和service的配置文件。
applicationContext.xml代碼主要如下:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- 數(shù)據(jù)訪問(wèn)層配置 -->
<import resource="classpath:/configSource/test-dao.xml" />
<!--服務(wù)層配置 -->
<import resource="classpath:/configSource/test-service.xml" />
</beans>
test-dao.xml代碼主要如下
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 掃描com.baobaotao.dao包下所有標(biāo)注@Repository的DAO組件 -->
<context:component-scan base-package="com.Integrat.*.dao"/>
</beans>
test-service.xml的配置內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 掃描com.baobaotao.service包下所有標(biāo)注@Service的服務(wù)組件 -->
<context:component-scan base-package="com.Integrat.*.service"/>
</beans>
test-dao.xml,test-service.xml 都是經(jīng)過(guò)了簡(jiǎn)單的配置,并不是完整的,請(qǐng)注意,只是演示一個(gè)配置的過(guò)程,一步步的配置,記下每一步的作用,了解下原理。
可以看到上面兩個(gè)配置文件每個(gè)里面都只有1行語(yǔ)句,就是自動(dòng)掃描注解的service和dao,如果這里不配置的話,在service和dao類上注解了也沒(méi)法解析,可能爆出bean創(chuàng)建錯(cuò)誤
注意賦值的時(shí)候,需要改下目錄結(jié)構(gòu)(com.Integrat.*.service 這里改成自己的service路徑*表示通配符)
配置好了就開(kāi)始寫(xiě)代碼吧,首先寫(xiě)dao層吧代碼如下:
@Repository
public class DemoDao extends BaseDao<AdminDicVO>{
public void demo(){
System.out.println("demoDAO執(zhí)行了
");
}
}
如果調(diào)用了dao層會(huì)簡(jiǎn)單在控制臺(tái)輸出一段話,值得注意的是在類名上面要寫(xiě)上注解@Repository ,表示這是dao層
接著寫(xiě)service層代碼如下:
@Service
public class DemoService{
@Autowired
private DemoDao dao;
public void Test(){
System.out.println("service調(diào)用dao層
");
dao.demo();
}
}
這里需要注意的是在類名上面需要寫(xiě)上@service注解,表示這是一個(gè)service,在private DemoDao dao; 上面要寫(xiě)上注解@Autowired,自動(dòng)注入這個(gè)dao,
接下來(lái)就看controller層代碼了:
@Controller
@RequestMapping("/demo")
public class DemoController {
@Autowired
private DemoService service; //這里注入service
@RequestMapping("/demo1")
public String demo(){
System.out.println("controller執(zhí)行demo,調(diào)用service
");
service.Test();
return null;
}
}
就這么簡(jiǎn)單的代碼就可以通過(guò)controller調(diào)用service,通過(guò)service調(diào)用dao層了
接下來(lái)就訪問(wèn)下http://localhost:8080/項(xiàng)目名/demo/demo1.test 就可以看到控制臺(tái)打印出的數(shù)據(jù)了。
這樣mvc3層就結(jié)合起來(lái)了。往后,只需要在在dao層加入hibernate,封裝basedao,在service層配置事務(wù),就ok了。
整合hibernate待續(xù)...
上篇講到了springmvc實(shí)現(xiàn)了控制層的helloworld實(shí)例,這篇接著寫(xiě)service層,和dao層的整合。
這里會(huì)需要幾個(gè)配置文件,applicationContext.xml,test-service.xml,test-dao.xml
applicationContext主要就是包含后面兩個(gè)文件了。其實(shí)這三個(gè)文件可以寫(xiě)在一個(gè)配置文件中,不過(guò)為了更好的分離,所以拆開(kāi)了dao的配置文件,和service的配置文件。
applicationContext.xml代碼主要如下:













test-dao.xml代碼主要如下













test-service.xml的配置內(nèi)容如下:














test-dao.xml,test-service.xml 都是經(jīng)過(guò)了簡(jiǎn)單的配置,并不是完整的,請(qǐng)注意,只是演示一個(gè)配置的過(guò)程,一步步的配置,記下每一步的作用,了解下原理。
可以看到上面兩個(gè)配置文件每個(gè)里面都只有1行語(yǔ)句,就是自動(dòng)掃描注解的service和dao,如果這里不配置的話,在service和dao類上注解了也沒(méi)法解析,可能爆出bean創(chuàng)建錯(cuò)誤
注意賦值的時(shí)候,需要改下目錄結(jié)構(gòu)(com.Integrat.*.service 這里改成自己的service路徑*表示通配符)
配置好了就開(kāi)始寫(xiě)代碼吧,首先寫(xiě)dao層吧代碼如下:








如果調(diào)用了dao層會(huì)簡(jiǎn)單在控制臺(tái)輸出一段話,值得注意的是在類名上面要寫(xiě)上注解@Repository ,表示這是dao層
接著寫(xiě)service層代碼如下:










這里需要注意的是在類名上面需要寫(xiě)上@service注解,表示這是一個(gè)service,在private DemoDao dao; 上面要寫(xiě)上注解@Autowired,自動(dòng)注入這個(gè)dao,
接下來(lái)就看controller層代碼了:














就這么簡(jiǎn)單的代碼就可以通過(guò)controller調(diào)用service,通過(guò)service調(diào)用dao層了
接下來(lái)就訪問(wèn)下http://localhost:8080/項(xiàng)目名/demo/demo1.test 就可以看到控制臺(tái)打印出的數(shù)據(jù)了。
這樣mvc3層就結(jié)合起來(lái)了。往后,只需要在在dao層加入hibernate,封裝basedao,在service層配置事務(wù),就ok了。
整合hibernate待續(xù)...