对于mybatis执行sql语句,需要用到的对象有:
- SqlSessionFactory对象,只有创建了SqlSessionFactory对象,才能调用openSession()方法得到SqlSession对象。
- dao接口的代理对象,例如UserDao接口,需要的代理对象为:SqlSeesion.getMapper(UserDao.class)。
- 数据源DataSource对象,使用一个更强大、功能更多的连接池对象代替mybatis自己的PooledDataSource。
1、创建数据库
create table t_user
(
uid int primary key auto_increment,
username varchar(20),
password varchar(20),
phone varchar(11),
address varchar(50)
);
insert into t_user(username,password,phone,address) values('张三','666','18965423548','南阳');
insert into t_user(username,password,phone,address) values('李四','333','18754263548','许昌');
insert into t_user(username,password,phone,address) values('小美','123','18565234759','信阳');
insert into t_user(username,password,phone,address) values('大壮','555','18565234759','开封');
select * from t_user;
2、创建项目并导入所需要jar包
3、创建实体类
package com.wen.bean;
public class User {
private Integer uid;
private String username;
private String password;
private String phone;
private String address;
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"uid=" + uid +
", username='" + username + '\'' +
", password='" + password + '\'' +
", phone='" + phone + '\'' +
", address='" + address + '\'' +
'}';
}
}
4、创建dao层的接口
package com.wen.dao;
import com.wen.bean.User;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserDao {
@Select("select * from t_user")
List<User> selectAll();
}
5、创建service接口及其实现类
service接口
package com.wen.service;
import com.wen.bean.User;
import java.util.List;
public interface UserService {
List<User> findAll();
}
service接口实现类
package com.wen.service.impl;
import com.wen.bean.User;
import com.wen.dao.UserDao;
import com.wen.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserDao userDao;
@Override
public List<User> findAll() {
return userDao.selectAll();
}
}
6、创建mybatis配置文件
由于整合时相关连接数据库
/
实体类起别名
/
扫描
mapper
文件等操作都在
spring
配置文件中定义,
所以此处只剩日志的配置
<?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>
<settings>
<setting name="logImpl" value="log4j"/>
</settings>
</configuration>
7、创建spring配置文件
<?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/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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--扫描component及同名注解-->
<context:component-scan base-package="com.wen"/>
<!--1.配置spring管理连接数据库的数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="factoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--引起数据源-->
<property name="dataSource" ref="dataSource"/>
<!--加载mybatis的核心配置文件-->
<property name="configLocation" value="mybatis.xml"/>
<!--给实体类起别名-->
<property name="typeAliasesPackage" value="com.wen.bean"/>
</bean>
<!--配置mapper扫描器,确定需要创建的代理对象-->
<bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wen.dao"/>
</bean>
<aop:aspectj-autoproxy/>
</beans>
8、创建测试类
package com.wen.servlet;
import com.wen.bean.User;
import com.wen.service.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class Test01 {
UserService service;
@Test
public void selectAll(){
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
service= context.getBean(UserService.class);
List<User> userList = service.findAll();
for (User user : userList) {
System.out.println(user);
}
}
}