使用工具IDEA 2017.2.3 MySql5.12数据库 MAVEN
引入所需依赖
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zking</groupId> <artifactId>DemoSSM</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>DemoSSM Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <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> <!--SpringMVC--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.10.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.10.RELEASE</version> </dependency> <!--MyBatis和Spring整合--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!--C3p0--> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.1</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>DemoSSM</finalName> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>数据库:
实体类:
package com.zking.entity; public class Person { private int pid; private String pname; private int page; private String psex; public Person() { } public Person(String pname, int page, String psex) { this.pname = pname; this.page = page; this.psex = psex; } public Person(int pid, String pname, int page, String psex) { this.pid = pid; this.pname = pname; this.page = page; this.psex = psex; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public String getPsex() { return psex; } public void setPsex(String psex) { this.psex = psex; } @Override public String toString() { return "Person{" + "pid=" + pid + ", pname='" + pname + '\'' + ", page=" + page + ", psex='" + psex + '\'' + '}'; } } dao层:实体类映射文件:package com.zking.dao; import com.zking.entity.Person; import java.util.List; public interface PersonDao { public void addPerson(Person person); public List<Person> getAll(); }mybatis_config.xml文件<?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> <package name="com.zking.entity"></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="scott"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/zking/entity/Person.xml"/> </mappers>--> </configuration>
<?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.zking.dao.PersonDao"> <select id="getAll" resultType="Person"> SELECT * from person </select> <insert id="addPerson" parameterType="com.zking.entity.Person"> INSERT INTO person(pname, page, psex) VALUE (#{pname},#{page},#{psex}) </insert> </mapper> 测试类:package com.zking.test; import com.zking.dao.PersonDao; import com.zking.entity.Person; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.util.List; public class TestMybatis { private SqlSession sqlSession; private SqlSessionFactory sqlSessionFactory; @Before public void befor(){ //读取配置文件,获取SqlSessionFactory try { sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml")); //通过SqlSessionFactory获取SqlSession sqlSession = sqlSessionFactory.openSession(); } catch (IOException e) { e.printStackTrace(); } } @After public void after(){ sqlSession.commit(); sqlSession.close(); } @Test public void test(){ PersonDao mapper = sqlSession.getMapper(PersonDao.class); // List<Person> person = mapper.getAll(); // for (Person person1 : person) { // System.out.println(person1); // } mapper.addPerson(new Person("小舞",18,"女")); } }经过测试类测试mybatis没有问题1.首先建立db.propertiesC3p0数据库连接池,然后就是mybatis与spring的整合了
uname=root upass=scott url=jdbc:mysql://localhost:3306/test driver_Class=com.mysql.jdbc.Driver initPoolSize=3 maxPoolSize=20
<?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> <!--配置数据源--> <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> <!--加载MyBatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <!--加载映射文件--> <property name="mapperLocations" value="classpath:com/zking/entity/*.xml"></property> </bean> <!--配置dao--> <!-- <bean id="personDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> <property name="mapperInterface" value="com.zking.dao.PersonDao"></property> </bean>--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.zking.dao"> </property> </bean> </beans> mybatis-config.xml文件可要可不要了,用测试类测试一下:package com.zking.test; import com.zking.dao.PersonDao; import com.zking.entity.Person; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; public class TestMybatisAndSpring { @Test public void test1(){ //读取spring配置文件 ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); PersonDao personDao = (PersonDao) ac.getBean("personDao"); List<Person> list = personDao.getAll(); for (Person person : list) { System.out.println(person); } personDao.addPerson(new Person("马可波罗",90,"男")); } }
运行成功!spring与mybatis整合没有问题!
最后一步:spring与springmvc的整合
1.新建controller类package com.zking.controller; import com.zking.dao.PersonDao; import com.zking.entity.Person; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class PersonController { @Autowired private PersonDao personDao; @RequestMapping("add") public String add(Person person){ personDao.addPerson(person); return "success"; } }2.springmvc配置文件<?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"> <context:component-scan base-package="com.zking.controller"> </context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>3.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> <!--加载spring配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 字符集过滤 --> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--加载SpringMVC--> <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> </web-app>界面:<%-- Created by IntelliJ IDEA. User: Administrator Date: 2018/1/13 0013 Time: 15:30 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="/add.action" method="post"> 姓名:<input type="text" name="pname"><br> 年龄:<input type="text" name="page"><br> 性别:<input type="text" name="psex"><br> <input type="submit" value="添加"> </form> </body> </html><%-- Created by IntelliJ IDEA. User: Administrator Date: 2018/1/13 0013 Time: 15:30 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> chenggong </body> </html>