在上一篇博客,引入mybatis框架的基础上,结合spring-mvc,实现代码的进一步简化。
操作:
1.新建maven的web工程:
项目整体视图:(后面会一个个新建,这里总览一下)
2.导包:
<!--数据库操作--> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <!--负责在容器中找东西--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.8.RELEASE</version> </dependency> <!--创建一个容器--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.8.RELEASE</version> </dependency> <!--读spring.xml new类--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.8.RELEASE</version> </dependency> <!--相当于servlet--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.8.RELEASE</version> </dependency> <!--连接池--> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!--mysql--> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!--事务管理器--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.8.RELEASE</version> </dependency> <!--用于事务管理器连接数据库--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.8.RELEASE</version> </dependency> <!--mybatis和spring连接桥梁--> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <!--自动转json--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.0</version> </dependency> <!--自动转json--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.0</version> </dependency> <!--自动转json--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.0</version> </dependency>
3.编写实体类及三层结构(service层、DAO层、控制层)
/** * 实体类 * */ public class Bank { private int id; private String name; private int money; //自行补全构造器、get/set方法、重写toString } /** * DAO接口层 * */ public interface BankDAO { public List<Bank> findAll(); public Bank findOne(int id); } /** * services层 * */ @Service // <bean id = "userService" class = "com.zyp.myssm.services.UserService"/> public class BankService { @Resource private BankDAO bdao; public List<Bank> findAll(){ return bdao.findAll(); } public Bank findOne(int id){ return bdao.findOne(id); } } /** * controller层 * */ @RestController // 全是servlet , 所有都返回json public class InitCtrl { // 不写名字, spring会自动用类型去获得对象getBean(UserService.class) @Resource private BankService bs; // 把每个方法看成一个小的servlet @RequestMapping("/init.do") public List<Bank> init() { return bs.findAll(); } @RequestMapping("/find.do") public Bank find(int id) { return bs.findOne(id); } }
4.配置文件
按照1步骤里的目录结构,新建好resources包,这里存放的都是所有配置文件。
1.> 其下的mapper包存放的xml文件,就是对应你实体类相应的sql执行语句(增删改查等)。
2.> 其下的spring.xml文件(注意与mapper同级!都在resources包下!!!不要写在mapper包里!!!),这里其实就是SSM框架中的spring-mvc,里面配置你链接数据库的驱动(同之前博客2中的mybatis)以及第三方spring-mybatis的bean(相当于Java中的类)。通过这里的配置信息,去映射到你的项目、数据源、整个DAO层
3.> webapp包下的WEB-INF下的web.xml文件,就是我们之前最初始的各个servlet,通过它的配置信息来扫描你的所有servlet(也就是我们Java类中的controller控制层)。
下面我们逐个创建:
1.在main包下,新建resources文件夹(与java文件夹同级),并且标记为Resourcces Root。
2.在resources包下新建mapper包,其下新建bank.xml文件,内容如下:(这里用查询举例,一个不带参数,一个带参数按照id查找。增删改同理)
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.zyp.myssm.dao.BankDAO"> <select id="findAll" resultType="com.zyp.myssm.entity.Bank"> select * from Bank </select> <select id="findOne" resultType="com.zyp.myssm.entity.Bank" parameterType="int"> select * from Bank where id=#{id} </select> </mapper>
3.在resources包下新建spring.xml文件(注意文件格式!!!)
内容如下:
<?xml version='1.0' encoding='UTF-8'?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:annotation-config></context:annotation-config> <context:component-scan base-package="com.zyp.myssm"></context:component-scan> <!--启动mvc--> <mvc:annotation-driven></mvc:annotation-driven> <!--以下是mybatis的链接数据库的整合--> <!--配置一个数据源--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://192.168.192.200:3306/mydemo?useSSL=false"></property> <property name="username" value="root"></property> <property name="password" value="okok"></property> <property name="initialSize" value="30"></property> <property name="maxActive" value="1000"></property> <property name="maxWait" value="1000"></property> <property name="minIdle" value="30"></property> </bean> <!--自动事务 可以让用户不commit--> <!--配置事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--自动提交配置--> <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> <!--配置spring和mybatis整合--> <!--这不是mybatis的类,是第三方的 mybatis-spring--> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="mapperLocations" value="classpath*:mapper/*.xml"></property> </bean> <!--扫描所有的dao接口--> <bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.zyp.myssm.dao"></property> </bean> </beans>
4.最后就是maven自带的web.xml文件(可能建的工程或者版本不一,没有就自己手动添加一个),内容如下:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.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.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
5.配置tomcat
进来后,点击左上角+号
下拉找到tomcat,选择本地
接下来逐个对照配置
然后选择Deployment项进行编辑
以上就是全部配置及代码。确保你的数据库能连接上,运行tomcat,弹出的页面后添加对应的servlet名称(推荐使用postman)。