赞
踩
目录
将web.xml文件中的内容用下面的代码替换掉
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
- version="4.0">
-
- </web-app>
- <dependencies>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>5.2.9.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>5.2.9.RELEASE</version>
- </dependency>
- <!--spring和mybatis整合的依赖-->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-spring</artifactId>
- <version>2.0.6</version>
- </dependency>
- <!--druid连接池-->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.2.4</version>
- </dependency>
- <!--springmvc-->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>5.2.9.RELEASE</version>
- </dependency>
- <!--mybatis的依赖-->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>3.5.6</version>
- </dependency>
- <!--mysql的依赖-->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.26</version>
- </dependency>
- <!--lombok-->
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>1.18.22</version>
- </dependency>
- <!--jackson-->
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.12.4</version>
- </dependency>
- <!--servlet和jsp-->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>2.5</version>
- </dependency>
- <dependency>
- <groupId>javax.servlet.jsp</groupId>
- <artifactId>jsp-api</artifactId>
- <version>2.2</version>
- </dependency>
- <!--log4j-->
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.17</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>1.7.30</version>
- </dependency>
- </dependencies>
- <?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"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
- <!--包扫描-->
- <context:component-scan base-package="com.rqk.controller"/>
- <!--静态资源放行-->
- <mvc:default-servlet-handler/>
- <!--特殊注解开启-->
- <mvc:annotation-driven/>
- <!--视图解析器-->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/views/"/>
- <property name="suffix" value=".jsp"/>
- </bean>
- </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:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
- <!--包扫描-->
- <context:component-scan base-package="com.rqk.service"/>
-
- <bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
- <!--定义数据源 连接数据库-->
- <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
- <property name="url" value="jdbc:mysql://localhost:3306/mydatis?serverTimezone=Asia/Shanghai"/>
- <property name="name" value="root"/>
- <property name="password" value=""/>
- <!--初始连接数-->
- <property name="initialSize" value="5"/>
- <!--最大连接数量-->
- <property name="maxActive" value="10"/>
- <!--连接超时等待时间 单位:毫秒-->
- <property name="maxWait" value="3000"/>
- </bean>
- <!--spring把mybatis配置文件的内容封装到SqlSessionFactoryBean类中-->
- <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <!-- mybatis配置文件-->
- <property name="dataSource" value="ds"/>
- <property name="mapperLocations" value="classpath:mapper/*.xml"/>
- </bean>
- <!--为指定到接口生成代理实现类。交于spring容器管理dao接口的代理实现类-->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.rqk.dao"/>
- </bean>
- </beans>
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
- version="4.0">
- <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:springmvc.xml</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>springmvc</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring.xml</param-value>
- </context-param>
-
- </web-app>
1. 业务层接口
- public interface Bookservice {
- public List<Book> findall();
- }
2.接口实现类
- @Service(value = "bookserviec")
- public class Bookserviecimpl implements Bookservice {
- @Autowired
- private BookDao bookDao;
-
-
- public List<Book> findall() {
-
- return bookDao.findall();
- }
- }
- @Controller
- @RequestMapping("/book")
- public class Bookcontroller {
- @Autowired
- private Bookservice bookserviec;
- @RequestMapping("/list")
- @ResponseBody
- public List<Book> list(){
- return bookserviec.findall();
- }
- }
数据库信息
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。