SSM整合

作者: 1baibai | 来源:发表于2021-12-15 18:26 被阅读0次

1、新建maven工程,添加spring、mybaties、Junit相关依赖

<?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.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>SpringMybatis</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- spring配置  log日志-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.9</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.5</version>
        </dependency>

        <!-- Mybatis mysql-connect durid连接池-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>

        <!-- spring-jdbc  mybatis-spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

2、创建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:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

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

    <!--配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driverClass}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${userName}"/>
        <property name="password" value="${password}"/>
    </bean>

    <!--  工厂配置  -->
    <bean id="sqlSessionFactor" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.baibai.domain"/>
        <property name="mapperLocations">
            <list>
                <value>mappers/*.xml</value>
            </list>
        </property>
        <property name="configLocation" value="mybaties.xml"/>
    </bean>

    <!--扫描dao-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactor"/>
        <property name="basePackage" value="com.baibai.dao"/>
    </bean>

</beans>

备注:mybaties.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="mapUnderscoreToCamelCase" value="true"/>
   </settings>
</configuration>

3、创建数据源配置properties文件

driverClass=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/work
userName=root
password=

4、创建Person实体文件,然后创建实体类对应的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.baibai.dao.PersonDao">

    <select id="list" resultType="Person">
        select * from peoples
    </select>
</mapper>

5、创建实体类对应的dao

package com.baibai.dao;

import com.baibai.domain.Person;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;

import java.util.List;

public interface PersonDao {
    List<Person> list();

    @Insert("insert into peoples(user_name,age,job) values(#{userName},#{age},#{job})")
    Boolean save(Person person);

    @Delete("delete from peoples where id > #{id}")
    boolean delete(Integer id);
}

6、基本配置完成

package com.baibai;

import com.baibai.dao.PersonDao;
import com.baibai.domain.Person;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.List;

public class Test {
    private static ClassPathXmlApplicationContext context;
    private static PersonDao personDao;

    static {
        context = new ClassPathXmlApplicationContext("applicationContext.xml");
        personDao = context.getBean("personDao", PersonDao.class);
    }

    @org.junit.Test
    public void getPersonList(){
        List<Person> list = personDao.list();
        System.out.println(list);
        System.out.println(list.size());
    }

    @org.junit.Test
    public void deletePerson(){
        System.out.println(personDao.delete(60209));
    }
}

项目结构预览:


image.png

相关文章

网友评论

      本文标题:SSM整合

      本文链接:https://www.haomeiwen.com/subject/lrupfrtx.html