Spring 基础环境
Spring 基础环境 指的是 Spring + MyBatis + 辅助类
1.引入依赖
<properties> <maven.compiler.source>22</maven.compiler.source> <maven.compiler.target>22</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>6.1.5</spring.version>
</properties> <dependencies> <!--spring web相关--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!--spring 数据库相关--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!--spring test相关--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>6.1.5</version> </dependency> <!--连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.22</version> </dependency> <!--MySQL驱动--> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>8.3.0</version> </dependency> <!--junit测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <!--日志--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>2.0.12</version> </dependency> <!--更方便的日志框架,log4j升级版--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.5.3</version> </dependency> <!--lombok注解处理--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.30</version> </dependency> <!--MyBatis-Plus依赖--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>3.5.4.1</version> </dependency> <!--spring整合mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>3.0.3</version> </dependency>
</dependencies>
2. 创建数据对应实体类和mapper以及映射文件
目录结构参考
对应的实体类
@Data
@AllArgsConstructor
public class User { private Long id; private String name; private Integer age; private String email;
}
对应的mapper和映射文件
mapper
:
public interface UserMapper { User selectById(Long id); }
mapper.xml
:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zxb.mp.mapper.UserMapper"> <select id="selectById" resultType="com.zxb.mp.pojo.User"> select * from user where id = #{id} </select>
</mapper>
3.创建数据库连接配置文件
jdbc.properties
:
4. 创建MyBatis-config
核心配置文件
详细可以参考:2.MyBatis环境搭建
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration> <properties resource="jdbc.properties"/> <typeAliases> <package name="com.zxb.mp.pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <package name="com.zxb.mp.mapper"/> </mappers>
</configuration>
5. 创建Spring的配置文件
resources\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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd"><!-- 引入jdbc.properties --><context:property-placeholder location="classpath:jdbc.properties"/><!-- 配置Druid数据源 --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean><!-- 配置用于创建SqlSessionFactory的工厂bean --><bean class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 设置MyBatis配置文件的路径(可以不设置) --><property name="configLocation" value="classpath:mybatis-config.xml"></property><!-- 设置数据源 --><property name="dataSource" ref="dataSource"/><!-- 设置类型别名所对应的包 --><property name="typeAliasesPackage" value="com.zxb.mp.pojo"></property><!--设置映射文件的路径若映射文件所在路径和mapper接口所在路径一致,则不需要设置--><!--<property name="mapperLocations" value="classpath:mapper/*.xml"></property>--></bean><!--配置mapper接口的扫描配置由mybatis-spring提供,可以将指定包下所有的mapper接口创建动态代理并将这些动态代理作为IOC容器的bean管理--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.zxb.mp.mapper"/></bean>
</beans>
6. 添加日志功能
resources\logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false"> <!--定义日志文件的存储地址 logs为当前项目的logs目录 还可以设置为../logs --> <property name="LOG_HOME" value="logs" /> <!--控制台日志, 控制台输出 --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符 宽度,%msg:日志消息,%n是换行符--> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> </encoder> </appender> <!--myibatis log configure--> <logger name="com.apache.ibatis" level="TRACE"/> <logger name="java.sql.Connection" level="DEBUG"/> <logger name="java.sql.Statement" level="DEBUG"/> <logger name="java.sql.PreparedStatement" level="DEBUG"/> <!-- 日志输出级别 --> <root level="DEBUG"> <appender-ref ref="STDOUT" /> </root>
</configuration>
测试
// 表示在Spring的环境中测试
@RunWith(SpringJUnit4ClassRunner.class)
// 指定Spring的配置文件
@ContextConfiguration("classpath:applicationContext.xml")
public class MyBatisPlusTest { private static final Logger log = LoggerFactory.getLogger(MyBatisPlusTest.class); // 自动注入mapper @Autowired private UserMapper userMapper; @Test public void testSelectById() { User user = userMapper.selectById(1L); log.info("user:{}", user); } }
Spring 基础环境加入MyBatis-Plus
修改配置文件spring配置文件的工厂bean
将:
<!-- 配置用于创建SqlSessionFactory的工厂bean -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 设置MyBatis配置文件的路径(可以不设置) --><property name="configLocation" value="classpath:mybatis-config.xml"></property><!-- 设置数据源 --><property name="dataSource" ref="dataSource"/><!-- 设置类型别名所对应的包 --><property name="typeAliasesPackage" value="com.zxb.mp.pojo"></property><!--设置映射文件的路径若映射文件所在路径和mapper接口所在路径一致,则不需要设置--><!--<property name="mapperLocations" value="classpath:mapper/*.xml"></property>-->
</bean>
改成:
<!-- 配置用于创建SqlSessionFactory的工厂bean -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 设置MyBatis配置文件的路径(可以不设置) --><property name="configLocation" value="classpath:mybatis-config.xml"></property><!-- 设置数据源 --><property name="dataSource" ref="dataSource"/><!-- 设置类型别名所对应的包 --><property name="typeAliasesPackage" value="com.zxb.mp.pojo"></property><!--设置映射文件的路径若映射文件所在路径和mapper接口所在路径一致,则不需要设置--><!--<property name="mapperLocations" value="classpath:mapper/*.xml"></property>-->
</bean>