1、新建一个javaEE web项目
2、加入相关依赖的坐标
<dependencies><!--数据系列:mybatis,mysgl,druid数据源,junit--><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.16</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.2</version></dependency><!-- 阿里数据源 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.10</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>provided</scope></dependency><!--spring系列:spring-context,aop,jdbc--><!-- spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.2.RELEASE</version></dependency><!-- spring-jdbc --><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.2.2.RELEASE</version></dependency><!--aop--><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>5.2.2.RELEASE</version></dependency>
</dependencies>
3、创建相应的包和类,以登录为例
package com.ffyc.ssm.model;import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;import java.util.Date;
@Data
public class Admin {private Integer id;private String account;private String password;@DateTimeFormat(pattern = "yyyy-MM-dd")private Date birthday;
}
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.4</version><scope>provided</scope>
</dependency>
@Data注解在类上,会为类的所有属性自动生成setter/getter、equals、canEqual、hashCode、toString方法,如为final属性,则不会为该属性生成setter方法。
@Service(value = "loginService")
@Transactional
public class LoginService {//注入dao层对象@AutowiredLoginDao loginDao;//注入的直接是接口的代理对象sqlsession.getMapper();public Admin login(Admin admin){Admin a=loginDao.Login(admin);return a;}
}
@Repository
public interface LoginDao {public Admin Login(Admin admin);
}
<?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.ffyc.ssm.dao.LoginDao"><select id="Login" resultType="com.ffyc.ssm.model.Admin">select * from admin where account=#{account} and password=#{password}</select>
</mapper>
4、创建并配置spring和mybatis配置文件
1.mybatis配置文件(mybatis-config.xml)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><settings> <!--常用设置--><!--启用日志功能,在运行时,可以将实际执行的sgL细节打印到控制台--><setting name="logImpl" value="STDOUT_LOGGING"/><setting name="mapUnderscoreToCamelCase" value="true"/><setting name="cacheEnabled" value="true"/> <!--全局开启二级缓存--></settings><typeAliases> <!--配置类型简称--><package name="com.ffyc.ssm.model"/></typeAliases>
</configuration>
2.数据库连接配置
mybatis-config.xml
classDriverName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/ssmdb?serverTimezone=Asia/Shanghai
uname=root
pwd=123456
db.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:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!--配置jdbc功能--><!--负责加载config.properties文件--><context:property-placeholder location="classpath:config.properties"></context:property-placeholder><!--spring统一管理数据库链接对象--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${classDriverName}"></property><property name="url" value="${url}"></property><property name="username" value="${uname}"></property><property name="password" value="${pwd}"></property><property name="initialSize" value="5"></property><property name="maxActive" value="10"></property></bean><!-- 配置spring事务管理类, 并注入数据源 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!--开启注解事务管理--><tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
3.spring集成mybatis
Spring 集成 Mybatis 其核心是将 SqlSessionFactory 交由 Spring 管理,并由 Spring 管理对 dao 接口的代理实现。
3.1 导入 mybatis jar 包
<!--spring集成mybatis框架-->
<dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.1</version>
</dependency>
3.2 配置 sqlSessionFactory,spring-mybatis.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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!--导入数据库连接 以及事务管理配置--><import resource="classpath:db.xml"></import> <!--target/classes下的--><!--让spring框架生成sqlSessionFactory--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!--注入数据源--><property name="dataSource" ref="dataSource"></property><!--配置mybatis配置文件--><property name="configLocation" value="classpath:mybatis-config.xml"></property><!--扫描SQL映射文件--><property name="mapperLocations" value="classpath:mappers/*Mapper.xml"></property></bean><!--让spring框架生成接口代理对象--><bean id="mapperFactory" class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.ffyc.ssm.dao"></property><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property></bean>
</beans>
4.spring.xml
spring.xml中需要导入spring-mybatis.xml,spring-mybatis.xml中导入db.xml
<!--spring.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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!--开启注解扫描--><context:component-scan base-package="com.ffyc.ssm"> </context:component-scan><!--开启自动代理--><aop:aspectj-autoproxy /><!--导入mybatis配置文件--><import resource="classpath:spring-mybatis.xml"></import></beans>
5、测试
public class Test {public static void main(String[] args) {ClassPathXmlApplicationContext app=new ClassPathXmlApplicationContext("spring.xml");LoginService loginService=app.getBean("loginService",LoginService.class);Admin admin=new Admin();admin.setAccount("admin");admin.setPassword("admin");Admin a=loginService.login(admin);System.out.println(a);}
}