基于注解的mybatis整合spring开发流程?

废话不多说,卷起袖子直接干。
(1)先上一张项目结构图
基于注解的mybatis整合spring开发流程?_第1张图片
(2)pom.xml【各个包的说明在注释】

<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.0modelVersion>
<groupId>cn.edu.hdugroupId>
<artifactId>MybatisartifactId>
<packaging>warpackaging>
<version>0.0.1-SNAPSHOTversion>
<name>Mybatis Maven Webappname>
<url>http://maven.apache.orgurl>
<dependencies>

<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>4.3.9.RELEASEversion>
dependency>

<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.1version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.3.0version>
dependency>

<dependency>
<groupId>commons-dbcpgroupId>
<artifactId>commons-dbcpartifactId>
<version>1.4version>
dependency>

<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.38version>
dependency>

<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>3.0.5.RELEASEversion>
dependency>

<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>4.3.9.RELEASEversion>
dependency> 

<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>4.3.9.RELEASEversion>
<scope>testscope>
dependency>
dependencies>
<build>
<finalName>MybatisfinalName>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
plugins>
build>
project>

(3)web.xml


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Mybatisdisplay-name>
  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>
  <servlet>
    <servlet-name>DispatcherServletservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:config/applicationContext.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
    <servlet-name>DispatcherServletservlet-name>
    <url-pattern>*.dourl-pattern>
  servlet-mapping>
web-app>

(4)spring配置文件applicationContext.xml


<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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    <context:component-scan base-package="dao" />
      
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="url" value="jdbc:mysql://localhost:3306/test"/>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:config/mybatis-spring.xml"/>
        <property name="mapperLocations" value="classpath:mapper/mapper.xml"/>
    bean>
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    bean>
beans>

(5)mybatis的配置文件mybatis-spring.xml




  <configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    settings>
    <typeAliases>
        <package name="entity"/>
    typeAliases>
  configuration>

(6)Navicat软件中创建mysql表【id设置成主键,且自增】,如图
基于注解的mybatis整合spring开发流程?_第2张图片
(7)mybatis的映射文件mapper.xml



<mapper namespace="dao.CityDao">
    <insert id="insertCity" parameterType="City">
        insert into china (id,name,province) values (
            #{id},
            #{name},
            #{province}
            )
    insert>
mapper>

(8)dao包下的接口CityDao

package dao;


import entity.City;

public interface CityDao {
    public void insertCity(City city);
}

基于注解的mybatis整合spring开发流程?_第3张图片

(9)entity包下的实体City

package entity;

public class City {
    private int id;
    private String name;
    private String province;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getProvince() {
        return province;
    }
    public void setProvince(String province) {
        this.province = province;
    }
}

基于注解的mybatis整合spring开发流程?_第4张图片
(10)测试类

package test;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import dao.CityDao;
import entity.City;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:config/applicationContext.xml"})//,"classpath:mapper/mapper.xml"
public class testDemo {
    @Autowired
    private CityDao cityDao;
    @Test
    public void insert(){
        try {
            City city = new City();
            city.setId(3);
            city.setName("嘉兴");
            city.setProvince("浙江");
            cityDao.insertCity(city);
            System.out.println("insert successfully");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
}

基于注解的mybatis整合spring开发流程?_第5张图片
(11)效果展示
show

你可能感兴趣的:(ssm框架)