最近搜索
暂无搜索记录
热搜
JAVA
大数据
分布式
Python
人工智能
爬虫
WEB
JavaScript
认证
文件结构如下:编译以后是有.xml的
卷 Data 的文件夹 PATH 列表
卷序列号为 6066-845B
D:\大学资料\马士兵\JAVA\SPRING6\SPRING6DEMO05
│ pom.xml
│
├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─snf
│ │ │ ├─mapper
│ │ │ │ UserMapper.java
│ │ │ │ UserMapper.xml
│ │ │ │
│ │ │ ├─pojo
│ │ │ │ User.java
│ │ │ │
│ │ │ └─service
│ │ │ │ UserService.java
│ │ │ │
│ │ │ └─impl
│ │ │ UserServiceImpl.java
│ │ │
│ │ ├─resources
│ │ │ applicationContext.xml
│ │ │
│ │ └─webapp
│ │ └─WEB-INF
│ │ web.xml
│ │
│ └─test
│ └─java
└─target
├─classes
│ │ applicationContext.xml
│ │
│ └─com
│ └─snf
│ ├─mapper
│ │ UserMapper.class
│ │ UserMapper.xml
│ │
│ ├─pojo
│ │ User.class
│ │
│ └─service
│ │ UserService.class
│ │
│ └─impl
│ UserServiceImpl.class
│
└─generated-sources
└─annotations
PS D:\大学资料\马士兵\Java\Spring6\Spring6Demo05>
这是我的配置文件:
<?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">
<!--【1】通过配置bean对象,给对象属性注入值来配置连接数据库数据源
DriverManagerDataSource属于spring-jdbc包-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/spring_mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true"></property>
<property name="username" value="root"></property>
<property name="password" value="0608"></property>
</bean>
<!--【2】配置SqlSessionFactory对象
SqlSessionFactoryBean属于mybatis-spring包-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.snf.pojo"></property>
</bean>
<!--【3】扫描UserMapper文件-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--需要注入工厂,因为以前都是:sqlSession.getMapper()-->
<property name="sqlSessionFactoryBeanName" value="factory"></property>
<!--扫描的文件-->
<property name="basePackage" value="com.snf.mapper"></property>
</bean>
</beans>
下面是我的Mapper内容:
package com.snf.mapper;
import com.snf.pojo.User;
/**
* @author 史宁芳
* @version 1.0
* @Date 2025/3/11 10:32
* @Description Spring6
*/
public interface UserMapper {
public abstract User selectOneUser(String username, String password);
}
下面是我的Mapper下的 xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.snf.mapper.UserMapper">
<select id="selectOneUser" resultType="User">
select * from user
where username=#{param1} and password=#{param2}
</select>
</mapper>