整合spring+springmvc+mybatis框架

xiaoxiao2021-02-28  7

首先,给大家看一下我整个的项目构造:

然后,开始我的整合:

1.首先,导入jar包(依赖):

<!--Junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!--MyBatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <!--MySQL--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.43</version> </dependency> <!--spring-webmvc包括核心包,所以只要导入这一个就好了--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.3.RELEASE</version> </dependency> <!--C3p0--> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.10.RELEASE</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!--返回json格式的数据--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.0</version> <scope>provided</scope> </dependency> <!-- json数据 使springMVC可以返回json值 ,视情况添加--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.30</version> </dependency>

然后,再往pom.xml里面加入一段配置,加载java文件夹下面的pom.xml文件:

<build> <finalName>g160828_ssm02</finalName> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>

2.我首先就是 先试试mybatis:

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!--设置别名--> <typeAliases> <!--<typeAlias type="com.desert.Dto.MyPerson" alias="a"></typeAlias>--> <package name="com.desert"></package> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/desert/entity/person.xml"/> </mappers> </configuration> 3.然后,再是我的实体类和xml文件:

Person:

private String pname; private int pid; private int page; Dao方法:

IPerson01:

public interface IPerson01 { public List<Person> getallPerson(Map map); public List<Person> getallPersonByPname(Map map); public Person getPersonById(int pid); public void delperson(int pid); public void addperson(Person person); public void updateperson(Person person); } 然后,再是我的person.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.desert.dao.IPerson01"> <select id="getPersonById" parameterType="int" resultType="com.desert.entity.Person"> select * from person where pid=#{pid} </select> <select id="getallPerson" resultType="com.desert.entity.Person" parameterType="map"> select * from person limit #{offset},#{pageSize} </select> <select id="getallPersonByPname" resultType="com.desert.entity.Person" parameterType="map"> select * from person p <where> <if test="pname != null"> and p.pname like '%${pname}%' </if> <if test="page != null"> and p.page =#{page} </if> </where> limit #{offset},#{pageSize} </select> <select id="delperson" parameterType="int"> delete from person where pid=#{pid} </select> <select id="addperson" parameterType="com.desert.entity.Person"> insert into person(pname,page) values(#{pname},#{page}) </select> <select id="updateperson" parameterType="com.desert.entity.Person"> update person set pname =#{pname}, page =#{page} where pid=#{pid} </select> </mapper> 测试一下:

@Test public void test01() throws IOException { SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config")); SqlSession sqlSession = sqlSessionFactory.openSession(); IPerson01 iPerson01=sqlSession.getMapper(IPerson01.class); //这里一些个sql动态查询: Map map=new HashMap(); //模糊查询 map.put("pname","E"); //分页: map.put("offset",0); map.put("pageSize",1); List<Person> la=iPerson01.getallPersonByPname(map); for (Person person : la) { System.out.println(person); } }

然后,再整合我的spring+mybatis:

4.创建一个 db.properties数据库连接池:

uname=root upass=root url=jdbc:mysql://localhost:3306/test driver_Class=com.mysql.jdbc.Driver initPoolSize=5 maxPoolSize=20 5.再创建一个applicationContext.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"> <!--加载db.properties--> <context:property-placeholder location="classpath:db.properties"></context:property-placeholder> <!--c3p0-数据源--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${uname}"></property> <property name="password" value="${upass}"></property> <property name="jdbcUrl" value="${url}"></property> <property name="driverClass" value="${driver_Class}"></property> <property name="initialPoolSize" value="${initPoolSize}"></property> <property name="maxPoolSize" value="${maxPoolSize}"></property> </bean> <!--配置SQLSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config"></property> <property name="mapperLocations" value="classpath:com/desert/entity/*.xml"></property> </bean> <!--这里是配置单个的dao,建议使用下面的那个方法,配置一个即可--> <!--配置Dao,Person01 <bean id="ipersonDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> <property name="mapperInterface" value="com.desert.dao.IPerson01"></property> </bean> --> <bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.desert.dao"></property> </bean> </beans> 写完这个,之前的那个mybatis-Context中的 <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/desert/entity/person.xml"/> </mappers>这些东西哦,就可以删除掉了。 6.然后测试 mybatis+spring测试一下,注意,事物什么的就不需要配置了,直接完事: @Test public void TestSpring(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); IPerson01 iPerson01= (IPerson01) applicationContext.getBean("ipersonDao"); Map map=new HashMap(); map.put("pname","E"); map.put("offset",0); map.put("pageSize",1); List<Person> la=iPerson01.getallPersonByPname(map); for (Person person : la) { System.out.println(person); } } 测试完毕,之后再整合 spring+springmvc+mybatis: 7.首先,得创建一个springmvc.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"> <!--配置扫描器--> <context:component-scan base-package="com.desert.controller"></context:component-scan> <!--视图解析器--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--前缀--> <property name="prefix" value="/"></property> <!--后缀--> <property name="suffix" value=".jsp"></property> </bean> <mvc:annotation-driven></mvc:annotation-driven> </beans> 8.然后,在我的controller包下面创建一个类: @Controller public class PersonController { //直接在这里定义一个IPerson01即可: @Autowired private IPerson01 iPerson01; @RequestMapping("getPerson") public String getPerson(){ Person person=iPerson01.getPersonById(1); System.out.println(person); return "success"; }}

9.再往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: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:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <!--这里是我的过滤器,用来转码的--> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>

10.配置完成之后,定义一个jsp界面:实现跳转拿数据:

我的person.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <a href="/getPerson.action">得到一个人</a> </body> </html>

然后,进入后台,能打印出东西,就表示你的框架基本搭建成功了!你就可以用这个框架开始书写你的项目了!!!!

转载请注明原文地址: https://www.6miu.com/read-1900139.html

最新回复(0)