<?xml version = “1.0” encoding = “UTF-8”?> <!DOCTYPE 配置 PUBLIC “ - // mybatis.org //DTD Config 3.0 // EN” < 配置> </ configuration > |
<?xml version = “1.0” encoding = “UTF-8”?> < beans xmlns = “http://www.springframework.org/schema/beans” xmlns:context = “http://www.springframework.org/schema/context” xmlns:p = “http://www.springframework.org/schema/p” xmlns:aop = “http://www.springframework.org/schema/aop” xmlns:tx = “http://www.springframework.org/schema/tx” 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/s ... ing-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http:/ /www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd “ > <! - 加载配置文件 - > < context:property-placeholder location = “classpath:db.properties” /> <! - 数据库连接池 - > < bean id = “dataSource” class = “org.apache.commons.dbcp.BasicDataSource” destroy-method = “close” > < property name = “driverClassName” value = “$ {jdbc.driver}” /> < property name = “url” value = “$ {jdbc.url}” /> < property name = “username” value = “$ {jdbc.username}” /> < property name = “password” value = “$ {jdbc.password}” /> < property name = “maxActive” value = “10” /> < property name = “maxIdle” value = “5” /> </ bean > <! - mapper配置 - > <! -让弹簧管理SqlSessionFactory中使用的MyBatis和弹簧整合包中的- > < bean id = “sqlSessionFactory” class = “org.mybatis.spring.SqlSessionFactoryBean” > <! - 数据库连接池 - > < property name = “dataSource” ref = “dataSource” /> <! -加载的MyBatis的全局配置文件- > < property name = “configLocation” value = “classpath:mybatis / SqlMapConfig.xml” /> </ bean > <! - 配置Mapper扫描器 - > < bean class = “org.mybatis.spring.mapper.MapperScannerConfigurer” > < property name = “basePackage” value = “cn.itcast.springmvc.mapper” /> </ bean > </ beans > |
jdbc.driver = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql:// localhost:3306 / springmvc ?characterEncoding = utf-8 jdbc.username = root jdbc.password = root |
<?xml version = “1.0” encoding = “UTF-8”?> < beans xmlns = “http://www.springframework.org/schema/beans” xmlns:context = “http://www.springframework.org/schema/context” xmlns:p = “http://www.springframework.org/schema/p” xmlns:aop = “http://www.springframework.org/schema/aop” xmlns:tx = “http://www.springframework.org/schema/tx” 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/s ... ing-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http:/ /www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd “ > <! - 事务管理器 - > < bean id = “transactionManager” class = “org.springframework.jdbc.datasource.DataSourceTransactionManager” > <! - 数据源 - > < property name = “dataSource” ref = “dataSource” /> </ bean > <! - 通知 - > < tx:advice id = “txAdvice” transaction-manager = “transactionManager” > < tx:attributes > <! - 传播行为 - > < tx:method name = “save *” propagation = “REQUIRED” /> < tx:method name = “insert *” propagation = “REQUIRED” /> < tx:method name = “delete *” propagation = “REQUIRED” /> < tx:method name = “update *” propagation = “REQUIRED” /> < tx:method name = “find *” propagation = “SUPPORTS”只读= “true” /> < tx:method name = “get *” propagation = “SUPPORTS”只读= “true” /> </ tx:attributes > </ tx:advice > <! - 切面 - > < aop:config > < aop:advisor advice-ref = “txAdvice” pointcut = “执行(* cn.itcast.springmvc.service。*。*(..))” /> </ aop:config > </ beans > |
<?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:p = “http://www.springframework.org/schema/p” xmlns:context = “http://www.springframework.org/schema/context” xmlns:dubbo = “http://code.alibabatech.com/schema/dubbo” xmlns:mvc = “http://www.springframework.org/schema/mvc” xsi:schemaLocation = “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/s ... ing-context-4.0.xsd “ > <! - 扫描带控制器注解的类 - > < context:component-scan base-package = “cn.itcast.springmvc.controller” /> <! - 加载注解驱动 - > < mvc:annotation-driven /> <! - 视图解析器 - > < bean class = “org.springframework.web.servlet.view.InternalResourceViewResolver” > < property name = “viewClass” 值= “org.springframework.web.servlet.view.JstlView” /> <! - jsp前缀 - > < property name = “prefix” value = “/ WEB-INF / jsp /” /> <! - jsp后缀 - > < property name = “suffix” value = “.jsp” /> </ bean > </ beans > |
<?xml version = “1.0” encoding = “UTF-8”?> < web-app xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xmlns = “http://java.sun.com/xml/ns/javaee” xsi:schemaLocation = “http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ” id = “WebApp_ID” version = “2.5” > < display-name > springmvc -web </ display-name > < welcome-file-list > < welcome-file > index.html </ welcome-file > < welcome-file > index.htm </ welcome-file > < welcome-file > index.jsp </ welcome-file > < welcome-file > default.html </ welcome-file > < welcome-file > default.htm </ welcome-file > < welcome-file > default.jsp </ welcome-file > </ welcome-file-list > <! - 加载spring容器 - > < context-param > < param-name > contextConfigLocation </ param-name > < param-value > classpath:spring / applicationContext- *。xml </ param-value > </ context-param > < listener > < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class > </ listener > < servlet > < servlet-name > springmvc </ servlet-name > < servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class > < init-param > < param-name > contextConfigLocation </ param-name > < param-value > classpath:spring / springmvc.xml </ param-value > </ init-param > </ servlet > < servlet-mapping > < servlet-name > springmvc </ servlet-name > < url-pattern > * .action </ url-pattern > </ servlet-mapping > </ web-app > |
公共接口 ItemService { List <Items> getItemsList(); } |
@服务 公共类 ItemServiceImpl 实现 ItemService { @ Autowired 私人 ItemMapper itemMapper ; @覆盖 public List <Items> getItemsList(){ List <Items> itemList = itemMapper .getItemList(); 返回 itemList ; } } |
@Controller 公共类 ItemController { @Autowired private ItemService itemService ; @RequestMapping (“/ itemList” ) public ModelAndView getItemList (){ List <Items> list = itemService .getItemsList(); ModelAndView modelAndView = new ModelAndView(); modelAndView .addObject(“itemList” ,list ); modelAndView .setViewName(“itemList” ); 返回 modelAndView ; } } |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |