springBoot+mybatis-plus用多了,就忘记最原始的配置,需要复习下
mybatis
项目结构
1.准备数据库
1.1创建数据库mybatis
1.2在库中创建表,并插入一条数据
CREATE TABLE `t_user` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int DEFAULT NULL,
`gender` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO `mybatis`.`t_user`(`id`, `name`, `age`, `gender`) VALUES (1, '张三', 34, '男');
2.创建maven项目,并引入依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.24</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
3.创建数据库表对应的实体类,mappe接口,xml
package com.mybatis.demo.entity;
import lombok.Data;
@Data
public class User {
private Integer id;
private String name;
private Integer age;
}
package com.mybatis.demo.mapper;
import com.mybatis.demo.entity.User;
import org.apache.ibatis.annotations.Param;
public interface UserMapper {
User getUserById(@Param("id") Integer id);
}
<?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">
<!-- namespace:命名空间,即对应的接口,resultType返回值类型-->
<mapper namespace="com.mybatis.demo.mapper.UserMapper">
<select id="getUserById" resultType="com.mybatis.demo.entity.User">
select * from t_user where id = #{id}
</select>
</mapper>
4.创建mybatis配置文件,配置数据库,配置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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="12345678"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/user.xml"/>
</mappers>
</configuration>
5.创建测试类,执行测试
import com.mybatis.demo.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class Test {
public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = null;
try {
sqlSession = sqlSessionFactory.openSession();
User user = sqlSession.selectOne("com.mybatis.demo.mapper.UserMapper.getUserById",1);
System.out.println(user);
}finally {
sqlSession.close();
}
}
}
Spring
基于xml
1.创建maven项目,导入依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
2.创建类Peson
package bean;
import lombok.Data;
@Data
public class Person {
private Integer age;
}
3.创建Bean配置文件
通过全类名配置,本质上就是反射调用无参构造方法
另外还可以通过实例工厂、静态工厂配置(就是调用工厂方法获取实例)
还可以通过FactoryBean配置(配置bena时用到了其他bean),类实现FactoryBean(就是调用getObject()获取实例)
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置bean,默认使用无参构造创建,用set方法注入属性
id设置bean名称
class设置bean类全路径
property配置属性值
-->
<bean id="zhangSan" class="bean.Person">
<property name="age" value="16"/>
</bean>
</beans>
4.测试类
import bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
//创建IO容器
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
//从容器中获取bean
Person zhangSan = (Person)context.getBean("zhangSan");
System.out.println(zhangSan.getAge());
}
}
基于注解
1.替换上面的xml即可,配置一个扫描的路径
2.类Person上加@Component注解
<?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
http://www.springframework.org/schema/context/spring-context.xsd ">
<context:component-scan base-package="bean"></context:component-scan>
</beans>