6个需要修改或创建的文件
UserMapper
package com.Li.mapper;import com.Li.pojo.User;import java.util.List;public interface UserMapper {public List selectUser();
}
UserMapper.xml
User
package com.Li.pojo;import lombok.Data;@Data
public class User {private int id;private String name;private String pwd;
}
mybatis-config.xml
MyTest
import com.Li.mapper.UserMapper;
import com.Li.pojo.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 org.junit.Test;import java.io.IOException;
import java.io.InputStream;
import java.util.List;public class MyTest {@Testpublic void selectUser() throws IOException {String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);SqlSession sqlSession = sqlSessionFactory.openSession();UserMapper mapper = sqlSession.getMapper(UserMapper.class);List userList = mapper.selectUser();for (User user: userList){System.out.println(user);}sqlSession.close();}}
pom.xml
spring-study com.Li 1.0-SNAPSHOT 4.0.0 spring-10-mybatis 8 8 junit junit 4.12 mysql mysql-connector-java 8.0.29 org.mybatis mybatis 3.5.2 org.springframework spring-webmvc 5.3.23 org.springframework spring-jdbc 6.0.0 org.aspectj aspectjweaver 1.9.4 org.mybatis mybatis-spring 2.0.7 org.projectlombok lombok 1.18.10 src/main/java **/*.properties **/*.xml false src/main/resources **/*.properties **/*.xml false
测试:
整合Mybatis方式一:(详解在代码中)
UserMapperImpl:(接口的实现类,用来实现 UserMapper的Mybatis)
package com.Li.mapper;import com.Li.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;import java.util.List;public class UserMapperImpl implements UserMapper{//作用是简化测试类//我们的所有操作,都使用sqlSession来执行,在原来,现在都使用SqlSessionTemplateprivate SqlSessionTemplate sqlSession;public void setSqlSession(SqlSessionTemplate sqlSession) {this.sqlSession = sqlSession;}@Overridepublic List selectUser() {UserMapper mapper = sqlSession.getMapper(UserMapper.class);return mapper.selectUser();}
}
mybatis-config.xml:(简化删减里面的内容,将里面的内容移至spring-dao.xml中)
spring-dao.xml(整合了mybatis-config.xml与spring-dao.xml,使两种xml可以互通)
Mytest:(以spring的方式来做)
import com.Li.mapper.UserMapper;
import com.Li.pojo.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 org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import java.io.IOException;
import java.io.InputStream;
import java.util.List;public class MyTest {@Testpublic void selectUser() 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);}}}
测试:
Error loading class [org.springframework.jdbc.datasource.DriverManagerDataSource] for bean with name
会出现这样的红字报错。
直接改jar包的版本。。。这tm花了我好长时间找这个错误。。。无语
org.springframework spring-jdbc 5.2.0.RELEASE
成功!
小结:你需要去对应MyTest的代码,来理解你每一步操作都是在干什么。
方式二:(简化方式一)
UserMapperImpl2:(继承实现)
package com.Li.mapper;import com.Li.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;import java.util.List;
//继承SqlSessionDaoSupport,不需要创建sqlSession了,可以直接用
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{@Overridepublic List selectUser() {SqlSession sqlSession = getSqlSession();UserMapper mapper = sqlSession.getMapper(UserMapper.class);return mapper.selectUser();}
}
applicationContext.xml:(将spring-dao.xml与mybatis-config.xml整合为一个)
修改之后测试成功!
重点在方式一的理解与掌握!