第一个Spring程序
第一个Mybatis程序
第一个Spring-Mybatis程序
第一个SpringBoot-Mybatis程序
1. Spring程序配置文件
- beans.xml:/resources
<!--注册一个Bean-->
<bean id="hello" class="com.kuang.pojo.Hello">
<property name="name" value="Spring"/>
</bean>
//使用Bean
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
//getBean : 参数即为spring配置文件中bean的id;
//就是将容器中“hello”这个JavaBean拿出来,注入到Hello hello中;
Hello hello = (Hello) context.getBean("hello");
hello.show();
2. Mybatis程序配置文件
- mybatis-config.xml:/resources
- mapper.xml:/resources/mapper
- 核心配置文件mybatis-config.xml需要注册mapper映射文件
<!--configuration核心配置文件mybatis-config.xml-->
<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/test?userSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="ily******"/>
</dataSource>
</environment>
</environments>
<!--在核心配置文件mybatis-config.xml注册Mapper.xml文件-->
<mappers>
<mapper resource="Dao/UserDao.xml"/>
</mappers>
</configuration>
<!--mapper.xml文件-->
<mapper namespace="Dao.UserDao">
<select id="getUserList" resultType="Pojo.User">
select * from USER
</select>
</mapper>
//使用配置文件执行mapper
@Test
public void test(){
//1.获取SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//2.执行SQL
UserDao userDao = sqlSession.getMapper(UserDao.class);
List<User> userList = userDao.getUserList();
for (User user : userList) {
System.out.println(user);
}
//关闭sqlSession
sqlSession.close();
}
3. Spring-Mybatis程序配置文件
- beans.xml:/resources
- mybatis-config.xml:/resources
- mapper.xml:/resources/mapper
- beans.xml需要注册mapper映射文件
<?xml version="1.0" encoding="GBK"?>
<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
<!--DataSource:使用Spring的数据源替换Mybatis的配置 c3p0 dbcp druid
我们这里使用Spring提供的JDBC:-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--关联mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/kuang/mapper/*.xml"/>
</bean>
<!--SqlSessionTemplate:就是我们使用的sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能使用构造器注入sqlSessionFactory,因为它没有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean id="userMapper" class="com.kuang.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
//测试
@Test
public void test () throws IOException {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
for (User user : userMapper.selectUser()) {
System.out.println(user);
}
}
4. SpringBoot-Mybatis程序配置文件
- application.yml(就是mybatis-config.xml核心配置文件)
-
- mapper.xml:/resources/mapper
- application.yml需要注册mapper映射文件
spring:
datasource:
name: pro_man_sys_db
url: jdbc:mysql://localhost:3306/pro_man_sys_db?serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
resources:
static-locations: classpath:/static,classpath:/resources,file:/root/uploadFiles
mybatis:
type-aliases-package: com.shiliuzi.model
mapper-locations: classpath:mapper/*.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--configuration core file-->
<mapper namespace="com.guo.mapper.UserMapper">
<select id="queryUserList" resultType="User">
</select>
</mapper>
//使用:直接创建一个controller层接口,使用http访问
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/list")
public List<User> List(){
return userMapper.queryUserList();
}
}