spring整合mybatis - 技术经验 - W3xue 亚博安卓app,亚博vip86.com ,博亚娱乐
亚博vip86.com 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:亚博vip86.com ???Java相关???Spring???查看文章
spring整合mybatis
来源:cnblogs  作者:_小灰灰  时间:2019/7/30 9:48:23  对本文有异议

项目的目录结构如下:

?

?

第一步:新建好一个maven项目,引入pom文件的依赖:

xml version="1.0" encoding="UTF-8"?>
<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.itcastgroupId>
      <artifactId>DemoMyartifactId>
      <packaging>warpackaging>
      <version>0.0.1-SNAPSHOTversion>
  
    <dependencies>


<dependency>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-coreartifactId>
<version>1.3.5version>
dependency>

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


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


<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>4.3.7.RELEASEversion>
<scope>testscope>
dependency>



<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-validatorartifactId>
<version>5.4.1.Finalversion>
dependency>


<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelperartifactId>
<version>5.0.0version>
dependency>



<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.8.8version>
dependency>




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


<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>4.3.7.RELEASEversion>
dependency>



<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.2version>
dependency>



<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.3.1version>
dependency>


<dependency>
    <groupId>c3p0groupId>
    <artifactId>c3p0artifactId>
    <version>0.9.1.2version>
dependency>


<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
    <version>8.0.12version>
dependency>
<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-beansartifactId>
    <version>4.3.7.RELEASEversion>
dependency>

<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.0.1version>
<scope>providedscope>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>RELEASEversion>
dependency>

dependencies>
<build>
 <plugins>
       <plugin>
             <groupId>org.apache.maven.pluginsgroupId>
             <artifactId>maven-compiler-pluginartifactId>
             <version>3.1version>
             <configuration>
                 <source>1.8source>     
                 <target>1.8target>      
             configuration>
       plugin>
  plugins>
build>
project>

第二部:新建pojo类:

package cn.itcast.mybatis.po;
import java.util.Date;
public class User {
    
    private static final long serialVersionUID = 1L;
    private Integer id;
    private String username;
    private String sex;
    private Date birthday;
    private String address;


    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", sex=" + sex
                + ", birthday=" + birthday + ", address=" + address + "]";
    }

    
}

第三步:新建UserMapper接口。

package cn.itcast.mybatis.po;

public interface UserMapper {
    public User findUserById(Integer id);

}

第四步:在src/main/resources下面新建文件夹mapper, 在mapper下新建UserMapper.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="cn.itcast.mybatis.po.UserMapper">

    
    <select id="findUserById" parameterType="Integer" resultType="User">
        select * from user where id = #{id}
    select>
       
mapper>

第四步:在resources下面新建spring的配置文件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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       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-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    
    <context:property-placeholder location="classpath:db.properties" />

    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.uid}"/>
        <property name="password" value="${jdbc.password}"/>
        
        <property name="initialPoolSize" value="10"/>
        
        <property name="minPoolSize" value="5"/>
        <property name="maxPoolSize" value="20"/>
     bean>

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
         
        <property name="configLocation" value="classpath:SqlMapConfig.xml" />
        
        <property name="dataSource" ref="dataSource" />
    bean>
    
    
      <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
           <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
            <property name="mapperInterface" value="cn.itcast.mybatis.po.UserMapper" />
    bean>
    
beans>

第五步:resources下配置数据库连接 db.properties:

#jdbc.driver=com.mysql.jdbc.Driver
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.1.20:3306/test?serverTimezone=UTC&useSSL=false
jdbc.uid=root
jdbc.password=root123

?

然后在resources下新建日志文件:log4j.properties

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

?

第六步:在resources下面新建mybatis的配置文件 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>
      
     
    <typeAliases>
        <typeAlias alias="User" type="cn.itcast.mybatis.po.User"/>
    typeAliases>
<mappers> <mapper resource="mapper/UserMapper.xml"/> mappers>


configuration>

第七部: 新建测试类:

@org.junit.Test
        public void func1() throws Exception {
            ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
            UserMapper mapper=ac.getBean(UserMapper.class);
            User user = mapper.findUserById(1);
            System.out.println(user);
        }

结果:

?

原文链接:http://www.cnblogs.com/2019lgg/p/11266245.html