java SSM第三章学习内容(Spring与Mybatis整合的方式一:SqlSessionTemplate,二:SqlsessionDaoSupport,三:删除实例化接口)

基本步骤:

1.导jar包到lib下并添加依赖
2.把applicationContext.xml及mybatis-config.xml整合到recources包下


一.第一种引用SqlSessionTemplate



1.创建数据库的实体类Lianxi.java
2.新建一个接口LianxiMapper.java,给于方法
3.新建一个接口的LianxiMapperxml,修改xml内容为SQL语句


4.新建一个类用于实现接口后,使用SqlSessionTemplate类给于getset方法并重写接口方法


5.修改mybatis-config.xml,留下别名即可

6.修改applicationContext.xml里面的配置

7.写测试类并调用实现接口类



参考代码及配置:

LianxiMapper.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.dao.LianxiMapper">

    <select id="selectAll" resultType="Lianxi">
        select * from Lianxi
    </select>
</mapper>




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>
    <!--类型别名 -->
    <typeAliases>
        <package name="com.entity" />
    </typeAliases>
</configuration>


applicationContext.xml

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

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"
            value="jdbc:mysql://127.0.0.1:3306/supermarket?
                        useUnicode=true&amp;characterEncoding=utf-8" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <property name="mapperLocations">
            <list>
                <value>classpath:com/dao/*.xml</value>
            </list>
        </property>
    </bean>
    
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
    
    <bean id="ss" class="com.dao.LianxiMapperImpl" 
        p:sqlSessionTemplate-ref="sqlSessionTemplate">
    </bean></beans>


实体类Lianxi:

public class Lianxi {
    private int id;
    private String model;
    private double ofprice;
    private Date ofDate;
    public Lianxi(int id, String model, double ofprice, Date ofDate) {
        super();
        this.id = id;
        this.model = model;
        this.ofprice = ofprice;
        this.ofDate = ofDate;
    }
    public Lianxi() {
        super();
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getModel() {
        return model;
    }
    public void setModel(String model) {
        this.model = model;
    }
    public double getOfprice() {
        return ofprice;
    }
    public void setOfprice(double ofprice) {
        this.ofprice = ofprice;
    }
    public Date getOfDate() {
        return ofDate;
    }
    public void setOfDate(Date ofDate) {
        this.ofDate = ofDate;
    }
}


LianxiMapper接口:

public interface LianxiMapper {
    public List<Lianxi> selectAll();
}

LianxiMapperImpl实现接口:

public class LianxiMapperImpl implements LianxiMapper{
    private SqlSessionTemplate sqlSessionTemplate;
    public SqlSessionTemplate getSqlSessionTemplate() {
        return sqlSessionTemplate;
    }
    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }
    public List<Lianxi> selectAll() {
        List<Lianxi> list = sqlSessionTemplate.selectList("com.dao.LianxiMapper.selectAll");
        return list;
    }

}


测试类:
public class Lianxi1Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        LianxiMapperImpl lianxiMapperImpl=(LianxiMapperImpl)context.getBean("ss");
        List<Lianxi> list = lianxiMapperImpl.selectAll();
        for (Lianxi li:list) {
            System.out.println(li.getId()+li.getModel());
        }
    }
}


二.SqlsessionDaoSupport




步骤一样,不同在接口实现类跟之前不同


LianxiMapperImpl代码,继承SqlSessionDaoSupport
public class LianxiMapperImpl extends SqlSessionDaoSupport implements LianxiMapper{
    public List<Lianxi> selectAll() {
        SqlSession session = super.getSqlSession();
        List<Lianxi> list = session.selectList("com.dao.LianxiMapper.selectAll");
        return list;
    }
}




三.第三种方法

不需要实例化接口(删除),然后修改applicationContext.xml文件配置




配置文件代码:
 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"
            value="jdbc:mysql://127.0.0.1:3306/supermarket?
                        useUnicode=true&amp;characterEncoding=utf-8" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>
    
    <bean id="lianxiMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" >
    <property name="mapperInterface" value="com.dao.LianxiMapper"/>
    <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
    
    <context:component-scan base-package="com.servicet"/>
    
    </beans>


 

猜你喜欢

转载自blog.csdn.net/JayVergil/article/details/81264804
今日推荐