这篇是上一篇的延续:
用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(一)
源代码在github上可以下载,地址:https://github.com/guoxia0719/ssm-select
这篇就根据实际文件进行梳理:
首先已经确定的文件有:Person PersonMapper PersonMapper.xml jdbc.properties
这些有的自动生成的文件方法较多,仅测试了其中一个方法,其他的没有去除,有兴趣的可以自己测试下效果。
工程的目录结构如下图所示:红色框框中的部分是此次需求需要用到的文件,每个文件都有部分说明
下面,针对每个文件进行下说明,说明的顺序和图中文件的列表顺序同:
PersonController文件:
package com.one.controller; import com.one.entity.Person; import com.one.service.PersonService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import javax.annotation.Resource; /** * @Author: guopengxia * @Date: 2019/3/17 13:02 * @Version: 1.0 */ @Controller @RequestMapping("/person") public class PersonController { @Resource PersonService personService; @RequestMapping(value="/select", produces = "text/plain;charset=utf-8") public ModelAndView getPerson(@RequestParam("id")String id){ Person p=personService.getPerson(Integer.parseInt(id)); ModelAndView mav=new ModelAndView("success"); mav.addObject("p",p); return mav; } }
文件中的@Controller表示可以用于匹配用户请求的url信息, @RequestMapping("/person")用于匹配具体的url的值,这个字段可以分开写到类上面一部分,写到方法上一部分
如上面的实例部分,也可以直接写到方法上(@RequestMapping("/person/select")),
PersonMapper.Java文件:
package com.one.dao; import com.one.entity.Person; public interface PersonMapper { int deleteByPrimaryKey(Integer id); int insert(Person record); Person selectByPrimaryKey(Integer id); //仅仅测试了这一个方法,其他的方法有兴趣可以自己做下测试 int updateByPrimaryKey(Person record); }
这个接口和PersonMapper映射文件是一一对应关系,接口中的方法可以绑定到映射文件中的SQL的ID上,二映射文件PersonMapper中的namespace是PersonMapper的路径名
com.one.entity.Person.selectByPrimaryKey可以定位到映射文件中namespace=com.one.entity.Person,对应的SQL的ID=selectByPrimaryKey,就可以具体执行SQL语句了
PersonMapper.xml文件:
<?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.one.dao.PersonMapper" > <resultMap id="BaseResultMap" type="com.one.entity.Person" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="name" property="name" jdbcType="VARCHAR" /> <result column="password" property="password" jdbcType="VARCHAR" /> <result column="remark" property="remark" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List" > id, name, password, remark </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from person where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from person where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.one.entity.Person" > insert into person (id, name, password, remark) values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR}) </insert> <update id="updateByPrimaryKey" parameterType="com.one.entity.Person" > update person set name = #{name,jdbcType=VARCHAR}, password = #{password,jdbcType=VARCHAR}, remark = #{remark,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER} </update> </mapper>
服务类的文件;
PersonService.java文件的内容是:
package com.one.service; import com.one.entity.Person; /** * @Author: guopengxia * @Date: 2019/3/17 15:08 * @Version: 1.0 */ public interface PersonService{ Person getPerson(int id); }
这个接口,主要是定义了对外提供的 接口方法,可以封装具体的操作实现,便于维护
服务接口的实现类:PersonServiceImpl.java
package com.one.service.impl; import com.one.dao.PersonMapper; import com.one.entity.Person; import com.one.service.PersonService; import org.springframework.stereotype.Service; import javax.annotation.Resource; /** * @Author: guopengxia * @Date: 2019/3/17 13:03 * @Version: 1.0 */ @Service("personService") public class PersonServiceImpl implements PersonService { @Resource PersonMapper personMapper; public Person getPerson(int id){ return personMapper.selectByPrimaryKey(id); } }
这个类中具体通过接口实现了业务逻辑功能,通过调用接口的方法进行查询用户的信息。
数据库信息的配置:
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.connectionURL=jdbc:mysql://localhost:3306/fis?useUnicode=true&characterEncoding=utf-8 jdbc.userId=root jdbc.password=#### #jdbc.driverLocation=E:\\Java\\MySQL\\mysql-connector-java-5.1.43\\mysql-connector-java-5.1.43\\mysql-connector-java-5.1.43-bin.jar ##Oracle 数据库驱动 #jdbc.driver=oracle.jdbc.driver.OracleDriver ##jdbc:mysql://连接地址:端口号(默认3306)/数据库名 #jdbc.url=jdbc:oracle:thin:@10.1.104.xx:1521:xxxx ##用户名 #jdbc.username=lis ##密码 #jdbc.password=hxkf#xxx ##连接池初始化连接 #jdbc.initialSize=10 ##连接池最大连接数 #jdbc.maxActive=100 ##连接池最大空闲连接 #jdbc.maxIdle=0 ##连接池最小空闲连接 #jdbc.minIdle=0 ##等待超时 #jdbc.maxWait=5000 # ##线程池 ##------------ Task ------------ #task.core_pool_size=5 #task.max_pool_size=100 #task.queue_capacity=1000 #task.keep_alive_seconds=60
上面的没有注释部分是此次工程中需要的,下面是配置线程池的一些信息,可以自动忽略
spring-mvc.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: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 "> <!-- 组件扫描@Controller,@Service组件对象 --> <context:component-scan base-package="com.one.service.impl;com.one.controller"></context:component-scan> <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> </bean> <!-- 定义跳转的文件的前后缀 ,视图模式配置 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/views/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 注解扫描 --> <mvc:annotation-driven></mvc:annotation-driven> </beans>
spring-mybatis.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: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 "> <!-- 引入jdbc配置文件 --> <context:property-placeholder location="classpath:jdbc.properties" /> <!--dbcp数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClass}" /> <property name="url" value="${jdbc.connectionURL}" /> <property name="username" value="${jdbc.userId}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 配置spring与mybatis结合 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描mapping.xml文件 --> <property name="mapperLocations" value="classpath*:com/one/mapper/*.xml"/> </bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.one.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- Spring事务控制(注解配置) --> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 这个必须配置,否则事物不起作用 --> <!-- 支持@Transactional注解支持 --> <!-- 带有@Transactional标记的方法会自动调用txManager管理事务 --> </beans>
这个文件中的下面是配置事务的,这次简单查询不涉及的事务,仅仅是简单的查询功能,所以可以把下面的事务自动忽略
index.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="index.jsp" name="fm" method="post"> username:<input type="text" name="username"/> password:<input type="password" name="password"/> <br> <input type="submit" name="submit" value="提交"/> </form> </body> </html>
响应文件success.jsp:
<%@ page language="java" contentType="text/html;charset=UTF-8" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Title</title> </head> <body> 用户信息查询成功!<br> 用户信息是: 用户id: ${requestScope.p.id} 用户名:${requestScope.p.name} 密码:${requestScope.p.password} 备注:${requestScope.p.remark} </body> </html>
最后是pom.xml文件中导入的依赖jar包,问内容如下所示:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.three</groupId> <artifactId>testweb</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>testweb Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.5.4</version> </dependency> <!--Mybatis Spring--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency> <!--slf4j--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.9.RELEASE</version> </dependency> </dependencies> <build> <finalName>testweb</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> <!-- mybatis逆向工程 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <!--配置文件的位置--> <configurationFile>src/main/resources/Personal-GeneratorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </pluginManagement> <resources> <resource> <directory>src/main/java</directory><!--所在的目录--> <includes><!--包括目录下的.properties,.xml文件都会扫描到--> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
至此,所有的文件都已经改造完毕,下面就是进行测试部分:
1.配置IDEA中的数据库部分,如下图所示,点击IDEA中的右边database:
选择MySQL后,进行图中配置:
配置成功以后,进行tomcat的配置,配置截图如下所示:按照红色框框中的进行配置
图中的部署部分的截图如下所示:
配置完以后,点击 apply ,最后OK即可。
下面就可以启动项目工程了,点击tomcat旁边的绿色三角,就可以启动工程了,出现篇幅一中所示结果:
http://localhost:8080/,运行后可以出现
查询后,结果如下所示:
到此,真个项目就结束了,不过下面还有一些经常出现的常见性错误,由于篇幅问题,在单独写一份。
路径连接如下: