Servlet+Mybatis整合

Servlet+Mybatis整合

1.步骤

1.创建Web工程
2.在pom.xml文件中引入依赖包
3.resources目录下引入资源包
4.编程mapper,service,model,controller即可
5.发布tomcat服务器中布署运行

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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>

  <groupId>com.sygroupId>
  <artifactId>0415_mybatis_serlvetartifactId>
  <version>1.0-SNAPSHOTversion>
  <packaging>warpackaging>

  <name>0415_mybatis_serlvet Maven Webappname>
  
  <url>http://www.example.comurl>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.8maven.compiler.source>
    <maven.compiler.target>1.8maven.compiler.target>
  properties>

  <dependencies>
    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>3.4.6version>
    dependency>

    
    <dependency>
      <groupId>log4jgroupId>
      <artifactId>log4jartifactId>
      <version>1.2.17version>
    dependency>

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

    
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.12version>
      <scope>testscope>
    dependency>

    
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <version>4.0.0version>
      <scope>providedscope>
    dependency>

    
    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>fastjsonartifactId>
      <version>1.2.58version>
    dependency>

    
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>jstlartifactId>
      <version>1.2version>
    dependency>
  dependencies>

  <build>
    <finalName>0415_mybatis_serlvetfinalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-clean-pluginartifactId>
          <version>3.1.0version>
        plugin>
        
        <plugin>
          <artifactId>maven-resources-pluginartifactId>
          <version>3.0.2version>
        plugin>
        <plugin>
          <artifactId>maven-compiler-pluginartifactId>
          <version>3.8.0version>
        plugin>
        <plugin>
          <artifactId>maven-surefire-pluginartifactId>
          <version>2.22.1version>
        plugin>
        <plugin>
          <artifactId>maven-war-pluginartifactId>
          <version>3.2.2version>
        plugin>
        <plugin>
          <artifactId>maven-install-pluginartifactId>
          <version>2.5.2version>
        plugin>
        <plugin>
          <artifactId>maven-deploy-pluginartifactId>
          <version>2.8.2version>
        plugin>
      plugins>
    pluginManagement>


      
      <resources>
        <resource>
          <directory>src/main/javadirectory>
          <includes>
            <include>**/*.xmlinclude>
          includes>
        resource>
        <resource>
          <directory>src/main/resourcesdirectory>
          <includes>
            <include>**/*.xmlinclude>
            <include>**/*.propertiesinclude>
          includes>
        resource>
      resources>

    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.mavengroupId>
        <artifactId>tomcat7-maven-pluginartifactId>
        <version>2.2version>
        <configuration>
          <uriEncoding>utf-8uriEncoding>
          <port>8080port>
          <path>/path>
        configuration>
      plugin>
    plugins>

  build>
project>

3.mybatis-config.xml配置





<configuration>
    
    <properties resource="db.properties">properties>

    
    <settings>
        <setting name="logImpl" value="LOG4J"/>

        
        <setting name="lazyLoadingEnabled" value="false">setting>

        
        <setting name="cacheEnabled" value="false">setting>
    settings>


    
    <typeAliases>
        

        
        <package name="model">package>
    typeAliases>

    
    <environments default="development">
        <environment id="development">
            
            <transactionManager type="JDBC">transactionManager>
            
            <dataSource type="POOLED">
                <property name="driver" value="${db_driver}">property>
                <property name="url" value="${db_url}">property>
                <property name="username" value="${db_user}">property>
                <property name="password" value="${db_password}">property>
            dataSource>
        environment>
    environments>

    
    <mappers>
        
       <package name="mapper">package>
    mappers>
configuration>

4.引入工具类

package tools;

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 java.io.InputStream;

public class MybatisUtil {

    private static SqlSessionFactory factory;

    static {
        try{
            InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
            //2.创建SqlSessionFactoryBuilder对像
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            //3.创建SqlSessionFactory对像
            factory = builder.build(inputStream);
            //释放builder
            builder = null;
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 获取session对像
     * @return
     */
    public static SqlSession openSession(){
        return factory.openSession();
    }
}

5.配置文件

db.properties

db_driver=com.mysql.jdbc.Driver
db_url=jdbc:mysql:///invitationmanage
db_user=root
db_password=root

log4j.properties

# Global logging configuration
log4j.rootLogger=debug, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

你可能感兴趣的:(Servlet+Mybatis整合)