大数据正式36

大数据正式36

MyBatis的接口形式

  • 注意两点
    1. 接口名---namespace值对应
    2. 方法名---id一致
  • 图解联系
  • 代码示例

    • UserMapper接口

      package com.peng.mapper;
      
      import java.util.List;
      
      import com.peng.pojo.User;
      
      public interface UserMapper {
          // 查询全部的User
          public List<User> findAll();
      }
      
    • UserMapper配置文件

      <?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.peng.mapper.UserMapper">
          <select id="findAll" resultType="User">
              select * from user
          </select>
      </mapper>
      
    • User实体类

      package com.peng.pojo;
      
      public class User {
          private int id;
          private String name;
          private int age;
      
          public User() {
              super();
          }
      
          public User(int id, String name, int age) {
              super();
              this.id = id;
              this.name = name;
              this.age = age;
          }
      
          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 int getAge() {
              return age;
          }
      
          public void setAge(int age) {
              this.age = age;
          }
      
          @Override
          public String toString() {
              return "编号:" + id + ",姓名:" + name + ",年龄:" + age;
          }
      
      }
      
    • Test测试

      package com.peng.test;
      
      import java.io.InputStream;
      import java.util.ArrayList;
      import java.util.List;
      
      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.Before;
      
      import com.peng.mapper.UserMapper;
      import com.peng.pojo.User;
      
      public class Test {
          SqlSession openSession = null;
      
          @Before
          public void before() throws Exception {
              // 加载配置文件
              InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
              // 创建会话工厂
              SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
              // 创建会话对象
              openSession = factory.openSession(true);// true每执行一条sql提交一次
          }
      
          @org.junit.Test
          public void findAllTest() throws Exception {
              // 查询
              UserMapper mapper = openSession.getMapper(UserMapper.class);
              List<User> selectList = mapper.findAll();
              for (User user : selectList) {
                  System.out.println(user);
              }
          }
      
      }
      

Spring与Mybatis的整合

  1. jar包
  2. bean
    1. 会话工厂
      1. 数据源
      2. mybatis的核心配置文件
      3. mapper.xml的配置文件
    2. 接口扫描器
      1. mapper接口所在的类
  3. 项目结构,jar包,数据库表(database:mybatis_db;table:user)
  4. 代码

    • com.peng.mapper

      • UserMapper

        package com.peng.mapper;
        
        import java.util.List;
        
        import org.springframework.stereotype.Repository;
        
        import com.peng.pojo.User;
        
        @Repository(value = "userMapper")
        public interface UserMapper {
            // 查询全部的User
            public List<User> findAll();
        }
        
    • com.peng.pojo

      • User

        package com.peng.pojo;
        
        public class User {
            private int id;
            private String name;
            private int age;
        
            public User() {
                super();
            }
        
            public User(int id, String name, int age) {
                super();
                this.id = id;
                this.name = name;
                this.age = age;
            }
        
            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 int getAge() {
                return age;
            }
        
            public void setAge(int age) {
                this.age = age;
            }
        
            @Override
            public String toString() {
                return "编号:" + id + ",姓名:" + name + ",年龄:" + age;
            }
        
        }
        
      • UserMapper

        <?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.peng.mapper.UserMapper">
            <select id="findAll" resultType="com.peng.pojo.User">
                select * from user
            </select>
        </mapper>
        
    • com.peng.service

      • UserService

        <?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.peng.mapper.UserMapper">
            <select id="findAll" resultType="com.peng.pojo.User">
                select * from user
            </select>
        </mapper>
        
      • UserServiceImpl

        package com.peng.service;
        
        import java.util.List;
        
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.annotation.Qualifier;
        import org.springframework.stereotype.Service;
        
        import com.peng.mapper.UserMapper;
        import com.peng.pojo.User;
        
        @Service(value = "userService")
        public class UserServiceImpl implements UserService {
            @Autowired
            @Qualifier(value = "userMapper")
            private UserMapper userMapper;
        
            @Override
            public List<User> findAll() {
                return userMapper.findAll();
            }
        
        }
        
    • com.peng.test

      • Test

        package com.peng.test;
        
        import java.util.List;
        
        import org.springframework.context.ApplicationContext;
        import org.springframework.context.support.ClassPathXmlApplicationContext;
        
        import com.peng.pojo.User;
        import com.peng.web.UserServlet;
        
        public class Test {
            @org.junit.Test
            public void findAllTest() {
                ApplicationContext ac = new ClassPathXmlApplicationContext(
                        "applicationContext.xml");
                UserServlet us = (UserServlet) ac.getBean("userServlet");
                List<User> findAll = us.findAll();
                for (User user : findAll) {
                    System.out.println(user);
                }
            }
        }
        
    • com.peng.web

      • UserServlet

        package com.peng.test;
        
        import java.util.List;
        
        import org.springframework.context.ApplicationContext;
        import org.springframework.context.support.ClassPathXmlApplicationContext;
        
        import com.peng.pojo.User;
        import com.peng.web.UserServlet;
        
        public class Test {
            @org.junit.Test
            public void findAllTest() {
                ApplicationContext ac = new ClassPathXmlApplicationContext(
                        "applicationContext.xml");
                UserServlet us = (UserServlet) ac.getBean("userServlet");
                List<User> findAll = us.findAll();
                for (User user : findAll) {
                    System.out.println(user);
                }
            }
        }
        
    • 配置文件

      • 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:aop="http://www.springframework.org/schema/aop" 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-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/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd ">
            <!-- 扫描包 -->
            <context:component-scan base-package="com.peng"></context:component-scan>
            <!-- 扫描注解 -->
            <context:annotation-config></context:annotation-config>
            <!-- sql的会话工厂 -->
            <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
                <!-- 引入数据源 -->
                <property name="dataSource" ref="c3p0dataSource"></property>
                <!-- 引入核心配置文件 -->
                <property name="configLocation" value="classpath:/sqlMapConfig.xml"></property>
                <!-- 引入映射文件 -->
                <property name="mapperLocations" value="classpath:/com/peng/pojo/*.xml"></property>
            </bean>
            <!-- Mapper接口的扫描器 -->
            <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
                <property name="basePackage" value="com.peng.mapper"></property>
            </bean>
            <!-- 配置文件 -->
            <bean
                class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                <property name="location" value="classpath:/jdbc_config.properties"></property>
            </bean>
            <!--c3p0 -->
            <bean id="c3p0dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <property name="driverClass" value="${jdbc.driver}"></property>
                <property name="jdbcUrl" value="${jdbc.url}"></property>
                <property name="user" value="${jdbc.user}"></property>
                <property name="password" value="${jdbc.password}"></property>
                <property name="minPoolSize" value="3"></property>      <!--最小连接数 -->
                <property name="initialPoolSize" value="5"></property>  <!-- 初始化连接数 -->
                <property name="acquireIncrement" value="3"></property> <!-- 每次增长的个数 -->
            </bean>
        </beans>
        
      • jdbc_config.properties

        jdbc.user=root
        jdbc.password=root
        jdbc.url=jdbc\:mysql\://localhost\:3306/mybatis_db
        jdbc.driver=com.mysql.jdbc.Driver
        
      • log4j.properties

        log4j.rootLogger=DEBUG, Console
        #Console
        log4j.appender.Console=org.apache.log4j.ConsoleAppender
        log4j.appender.Console.layout=org.apache.log4j.PatternLayout
        log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
        log4j.logger.java.sql.ResultSet=INFO
        log4j.logger.org.apache=INFO
        log4j.logger.java.sql.Connection=DEBUG
        log4j.logger.java.sql.Statement=DEBUG
        log4j.logger.java.sql.PreparedStatement=DEBUG         
        
      • sqlMapConfig.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>
        </configuration>      
        
    • 测试结果

      编号:8,姓名:a,年龄:11
      编号:9,姓名:a,年龄:11
      编号:10,姓名:a,年龄:11
      编号:11,姓名:a,年龄:11
      编号:12,姓名:a,年龄:11
      编号:13,姓名:a,年龄:11
      编号:14,姓名:a,年龄:11
      编号:15,姓名:a,年龄:11        
      

MyBatis缓存

  • 一级缓存
    • 默认开启,第二次查询用第一次留下的缓存
  • 二级缓存【只有session关闭的时候才会缓存;修改数据库后缓存会被清除】

    • 默认关闭
    • 打开

      1. 核心配置文件当中开启缓存(二级缓存的总开关)

        <?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>
            <!--缓存 -->
            <settings>
                <setting name="cacheEnabled" value="true"></setting>
            </settings>
            <!-- 别名 -->
            <typeAliases>
                <typeAlias type="com.peng.pojo.User" alias="User" />
            </typeAliases>
            <!-- 配置数据源 -->
            <environments default="mysql">
                <environment id="mysql">
                    <transactionManager type="JDBC" />
                    <dataSource type="POOLED">
        
                        <property name="driver" value="com.mysql.jdbc.Driver" />
                        <property name="url"
                            value="jdbc:mysql://localhost:3306/mybatis_db?characterEncoding=utf-8" />
                        <property name="username" value="root" />
                        <property name="password" value="root" />
                    </dataSource>
                </environment>
        
                <environment id="oracle">
                    <transactionManager type="JDBC" />
                    <dataSource type="POOLED">
                        <property name="driver" value="oracle.jdbc.driver.OracleDriver" />
                        <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:XE" />
                        <property name="username" value="ht1602" />
                        <property name="password" value="htdb" />
                    </dataSource>
                </environment>
            </environments>
        
        
            <!-- 映射文件 -->
            <mappers>
                <mapper resource="com/peng/pojo/UserMapper.xml" />
            </mappers>
        
        </configuration>
        
      2. 映射文件中的开关(当前表)

        <?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.peng.mapper.UserMapper">
            <cache></cache>
            <select id="findAll" resultType="User">
                select * from user
            </select>
        </mapper>
        
      3. 必须让缓存的对象实现Serializable接口

        package com.peng.pojo;
        
        import java.io.Serializable;
        
        @SuppressWarnings("serial")
        public class User implements Serializable {
            private int id;
            private String name;
            private int age;
        
            public User() {
                super();
            }
        
            public User(int id, String name, int age) {
                super();
                this.id = id;
                this.name = name;
                this.age = age;
            }
        
            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 int getAge() {
                return age;
            }
        
            public void setAge(int age) {
                this.age = age;
            }
        
            @Override
            public String toString() {
                return "编号:" + id + ",姓名:" + name + ",年龄:" + age;
            }
        
        }
        
    • 测试类中session必须关闭

      package com.peng.test;
      
      import java.io.InputStream;
      import java.util.List;
      
      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.Before;
      
      import com.peng.mapper.UserMapper;
      import com.peng.pojo.User;
      
      public class Test {
          SqlSession openSession = null;
          SqlSessionFactory factory = null;
      
          @Before
          public void before() throws Exception {
              // 加载配置文件
              InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
              // 创建会话工厂
              factory = new SqlSessionFactoryBuilder().build(in);
              // 创建会话对象
              openSession = factory.openSession(true);// true每执行一条sql提交一次
          }
      
          @org.junit.Test
          public void findAllTest() throws Exception {
              // 查询
              UserMapper mapper = openSession.getMapper(UserMapper.class);
              List<User> selectList = mapper.findAll();
              for (User user : selectList) {
                  System.out.println(user);
              }
              openSession.close();
              // 查询
              SqlSession openSession2 = factory.openSession(true);
              UserMapper mapper1 = openSession2.getMapper(UserMapper.class);
              List<User> selectList1 = mapper1.findAll();
              for (User user : selectList1) {
                  System.out.println(user);
              }
          }
      
      }
      

手动封装结果集【细节:和setter方法名有关,不管属性名到底是什么;主键必须【写】

  • 属性名和字段名不一致

    • sql别名

      select id uid,name uname from user;
      
    • 配置文件中的别名

      <?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.peng.mapper.UserMapper">
          <resultMap type="com.peng.pojoUser" id="userMap">
              <id column="id" property="u_id" /><!--必须有id-->
              <result column="name" property="u_name" />
          </resultMap>
          <select id="oneByone" resultMap="userMap">
             select * from user
          </select>
      </mapper>
      
  • 一对一【association】

    • 多表查询,必须手动封装数据集
    • <?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.peng.mapper.UserMapper">
          <!-- 多表查询 -->
          <resultMap type="User" id="userMap">
              <id column="id" property="id" />
              <result column="name" property="name" />
              <result column="age" property="age" />
              <!-- 内置对象 -->
              <association property="userinfo" javaType="com.peng.pojo.UserInfo">
                  <result column="uid" property="uid" />
                  <result column="nick" property="nick" />
                  <result column="uage" property="uage" />
                  <result column="phone" property="phone" />
                  <!-- 内置对象 -->
                  <association property="parentUserInfo" javaType="com.peng.pojo.UserInfo">
                      <result column="p_uid" property="uid" />
                      <result column="p_nick" property="nick" />
                      <result column="p_uage" property="uage" />
                      <result column="p_phone" property="phone" />
                  </association>
              </association>
          </resultMap>
          <select id="oneByone" resultMap="userMap">
              SELECT * FROM
              USER u
              LEFT JOIN
              (SELECT * FROM
              user_info ui
              LEFT JOIN
              (SELECT uid p_uid,nick p_nick,uage
              p_uage,phone p_phone FROM user_info) pui
              ON
              ui.parent_id=pui.p_uid) yt
              ON
              u.id=yt.p_uid;
          </select>
      </mapper>
      
  • 一对多【collection】

    • 多表查询,必须手动封装数据集
    • <?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.peng.mapper.DeptMapper">
          <!-- 一对多表查询 -->
          <resultMap type="com.peng.pojo.Dept" id="deptMap">
              <id column="did" property="did" />
              <result column="dname" property="dname" />
              <collection property="userinfolist" ofType="com.peng.pojo.UserInfo">
                  <result column="uid" property="uid" />
                  <result column="nick" property="nick" />
                  <result column="uage" property="uage" />
                  <result column="phone" property="phone" />
              </collection>
          </resultMap>
          <select id="oneBymore" resultMap="deptMap">
              SELECT * FROM
              dept
              INNER JOIN
              user_info
              ON
              user_info.dedp_id=dept.did
              AND
              user_info.dedp_id IN(SELECT
              did FROM dept WHERE dname='法师部')
          </select>
      </mapper>
      
  • 多对多

    • 多表查询,必须手动封装数据集
    • 写两个一对多

spring+spring+mybatis整合

  • 概览
  • jar
  • 数据库
    • 数据库名称
      • mybatis_db
    • 表名称
      • user
    • 表结构
  • 实现

    • com.peng.controller

      • UserServlet

        package com.peng.controller;
        
        import java.util.List;
        
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.annotation.Qualifier;
        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.RequestMapping;
        
        import com.peng.pojo.User;
        import com.peng.service.UserService;
        
        @Controller(value = "userServlet")
        public class UserServlet {
            @Autowired
            @Qualifier(value = "userService")
            private UserService userService;
        
            @RequestMapping("/findAll.action")
            public String findAllAction() {
                List<User> users = userService.findAll();
                System.out.println(users);
                return "message";
            }
        }
        
    • com.peng.mapper

      • UserMapper

        package com.peng.mapper;
        
        import java.util.List;
        
        import org.springframework.stereotype.Repository;
        
        import com.peng.pojo.User;
        
        @Repository(value = "userMapper")
        public interface UserMapper {
            public List<User> findAll();
        }
        
    • com.peng.pojo

      • User

        package com.peng.pojo;
        
        public class User {
            private int id;
            private String name;
            private int age;
        
            public User() {
                super();
            }
        
            public User(int id, String name, int age) {
                super();
                this.id = id;
                this.name = name;
                this.age = age;
            }
        
            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 int getAge() {
                return age;
            }
        
            public void setAge(int age) {
                this.age = age;
            }
        
            @Override
            public String toString() {
                return "【编号:" + id + "名称:" + name + "年龄:" + age+"】";
            }
        
        }
        
      • UserMapper

        <?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.peng.mapper.UserMapper">
            <select id="findAll" resultType="com.peng.pojo.User">
                select * from user
            </select>
        </mapper>
        
    • com.peng.service

      • UserService

        package com.peng.service;
        
        import java.util.List;
        
        import com.peng.pojo.User;
        
        public interface UserService {
            public List<User> findAll();
        }
        
      • UserServiceImpl

        package com.peng.service;
        
        import java.util.List;
        
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.annotation.Qualifier;
        import org.springframework.stereotype.Service;
        
        import com.peng.mapper.UserMapper;
        import com.peng.pojo.User;
        
        @Service(value = "userService")
        public class UserServiceImpl implements UserService {
            @Autowired
            @Qualifier(value = "userMapper")
            private UserMapper userMapper;
        
            public List<User> findAll() {
                return userMapper.findAll();
            }
        
        }
        
    • 配置文件

      • applicationContext-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:xsi="http://www.w3.org/2001/XMLSchema-instance"
            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/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd ">
            <!-- 扫描包 -->
            <context:component-scan base-package="com.peng"></context:component-scan>
            <!-- 扫描注解 -->
            <context:annotation-config></context:annotation-config>
            <!-- 资源解析器:前后缀 -->
            <bean
                class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix" value="/WEB-INF/">
                </property>
                <property name="suffix" value=".jsp"></property>
            </bean>
            <!-- 文件上传资源解析器 -->
            <bean id="multipartResolver"
                class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
                <property name="defaultEncoding" value="utf-8"></property>
                <property name="maxUploadSize" value="10485760000"></property>
                <property name="maxInMemorySize" value="40960"></property>
            </bean>
        </beans>
        
      • 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:aop="http://www.springframework.org/schema/aop" 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-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/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd ">
            <!-- 扫描包 -->
            <context:component-scan base-package="com.peng"></context:component-scan>
            <!-- 扫描注解 -->
            <context:annotation-config></context:annotation-config>
            <!-- sql的会话工厂 -->
            <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
                <!-- 引入数据源 -->
                <property name="dataSource" ref="c3p0dataSource"></property>
                <!-- 引入核心配置文件 -->
                <property name="configLocation" value="classpath:/sqlMapConfig.xml"></property>
                <!-- 引入映射文件 -->
                <property name="mapperLocations" value="classpath:/com/peng/pojo/*.xml"></property>
            </bean>
            <!-- Mapper接口的扫描器 -->
            <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
                <property name="basePackage" value="com.peng.mapper"></property>
            </bean>
            <!-- 配置文件 -->
            <bean
                class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                <property name="location" value="classpath:/jdbc_config.properties"></property>
            </bean>
            <!--c3p0 -->
            <bean id="c3p0dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <property name="driverClass" value="${jdbc.driver}"></property>
                <property name="jdbcUrl" value="${jdbc.url}"></property>
                <property name="user" value="${jdbc.user}"></property>
                <property name="password" value="${jdbc.password}"></property>
                <property name="minPoolSize" value="3"></property>      <!--最小连接数 -->
                <property name="initialPoolSize" value="5"></property>  <!-- 初始化连接数 -->
                <property name="acquireIncrement" value="3"></property> <!-- 每次增长的个数 -->
            </bean>
        </beans>
        
      • jdbc_config.properties

        jdbc.user=root
        jdbc.password=root
        jdbc.url=jdbc\:mysql\://localhost\:3306/mybatis_db
        jdbc.driver=com.mysql.jdbc.Driver
        
      • log4j.properties

        log4j.rootLogger=DEBUG, Console
        #Console
        log4j.appender.Console=org.apache.log4j.ConsoleAppender
        log4j.appender.Console.layout=org.apache.log4j.PatternLayout
        log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
        log4j.logger.java.sql.ResultSet=INFO
        log4j.logger.org.apache=INFO
        log4j.logger.java.sql.Connection=DEBUG
        log4j.logger.java.sql.Statement=DEBUG
        log4j.logger.java.sql.PreparedStatement=DEBUG            
        
      • sqlMapConfig.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>
        </configuration>            
        
    • WebRoot

      • WEB-INF

        • sessage.jsp

          <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
          <head>
          <title>My JSP 'index.jsp' starting page</title>
          </head>
          
          <body>
              hello xy~~
              <br>
          </body>
          </html>
          
        • web.xml

          <?xml version="1.0" encoding="UTF-8"?>
          <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
              http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
              <!-- 解决乱码过滤器(只能解决post的,get的需要手动解决) -->
              <!-- 乱码过滤器 -->
              <filter>
                  <filter-name>characterFilter</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>characterFilter</filter-name>
                  <url-pattern>/*</url-pattern>
              </filter-mapping>
              <!-- 分发器 -->
              <servlet>
                  <servlet-name>springmvc_dispatcher</servlet-name>
                  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                  <init-param>
                      <param-name>contextConfigLocation</param-name>
                      <param-value>classpath:/applicationContext*.xml</param-value><!-- 注:这里的* -->
                  </init-param>
              </servlet>
              <!-- 分发器的映射 -->
              <servlet-mapping>
                  <servlet-name>springmvc_dispatcher</servlet-name>
                  <url-pattern>*.action</url-pattern>
              </servlet-mapping>
              <!-- 欢迎界面 -->
              <welcome-file-list>
                  <welcome-file>index.jsp</welcome-file>
              </welcome-file-list>
          </web-app>
          
      • index.jsp

        <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <html>
        <head>
        
        <title>My JSP 'index.jsp' starting page</title>
        </head>
        
        <body>
            This is my JSP page.
            <br>
        </body>
        </html>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乘风御浪云帆之上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值