其他分享
首页 > 其他分享> > Mybatis-Spring

Mybatis-Spring

作者:互联网

Mybatis-spring  用spring框架来实现mybatis

  首先创建一个person数据库

create table person
(
    name     varchar(45) null,
    age      int         null,
    password varchar(45) null,
    sdept    varchar(45) null
);
Person数据库

 

 

   

  然后创建person实体类放在pojo包下

package com.zhang.pojo;

public class Person {

    private String name;
    private long age;
    private String password;
    private String sdept;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public long getAge() {
        return age;
    }

    public void setAge(long age) {
        this.age = age;
    }


    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


    public String getSdept() {
        return sdept;
    }

    public void setSdept(String sdept) {
        this.sdept = sdept;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", password='" + password + '\'' +
                ", sdept='" + sdept + '\'' +
                '}';
    }
}
Person类

  在resource包下创建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>
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <typeAliases>
        <typeAlias type="com.zhang.pojo.Person" alias="person"/>
    </typeAliases>
</configuration>
mybatis-config.xml

  在Mapper包下创建PersonMapper接口

public interface PersonMapper {
    List<Person> getPersonList();
}
PersonMapper接口

  在resource包下创建beans.xml来实现spring框架

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="url"
                  value="jdbc:mysql://localhost:3306/spring_mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="username" value="root"/>
        <property name="password" value="zhang1234"/>
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="com/zhang/Mapper/PersonMapper.xml"/>
        <property name="configLocation" value="mybatis-config.xml"/>
    </bean>
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
    <bean id="PersonMapper" class="com.zhang.Mapper.PersonMapperImpl">
        <property name="sqlSessionTemplate" ref="sqlSession"/>
    </bean>
</beans>
beans.xml

  在Mapper包下创建PersonMapperImpl实现

package com.zhang.Mapper;

import com.zhang.pojo.Person;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

public class PersonMapperImpl implements PersonMapper {
    private SqlSessionTemplate sqlSessionTemplate;

    public SqlSessionTemplate getSqlSessionTemplate() {
        return sqlSessionTemplate;
    }

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }

    @Override
    public List<Person> getPersonList() {
        PersonMapper mapper = sqlSessionTemplate.getMapper(PersonMapper.class);
        List<Person> personList = mapper.getPersonList();
        return personList;
    }
}
PersomMapperImpl

  在Mapper报下创建PersonMapper.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="com.zhang.Mapper.PersonMapper">
    <select id="getPersonList" resultType="person">
        select * from person;
    </select>
</mapper>
PersonMapper.xml

  在test目录下创建Test类测试结果

public class Test {
    @org.junit.Test
    public void getPeronList(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        PersonMapperImpl personMapper = context.getBean("PersonMapper", PersonMapperImpl.class);
        List<Person> personList = personMapper.getPersonList();
        for (int i = 0; i < personList.size(); i++) {
            System.out.println(personList.get(i));
        }
    }
}
Test类

  测试日志以及结果

 

标签:sdept,String,Spring,PersonMapper,Mybatis,password,public,name
来源: https://www.cnblogs.com/Sum-muji/p/15140605.html