最近在学习SSM,刚好学习到Spring整合Mybatis了,随后网上大部分教程讲的都是通过 XML注解方式进行整合,但是让我一个 注解用习惯的开发者不是很愉快,随后特地写下该文章,防止各位花费大量时间踩坑
Maven导入相关坐标
<dependencies>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.32version>
dependency>
<dependency>
<groupId>com.mchangegroupId>
<artifactId>c3p0artifactId>
<version>0.9.5.2version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.3.18version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>5.3.10version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>5.3.10version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.3.10version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.0.1version>
<scope>providedscope>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>2.12.5version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.12.5version>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>javax.servlet.jsp-apiartifactId>
<version>2.2.1version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>5.3.18version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.3.18version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.20version>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.6version>
dependency>
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.4version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>compilescope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>5.3.18version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.9.7version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.6version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelperartifactId>
<version>5.1.4version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.5version>
dependency>
<dependency>
<groupId>com.github.jsqlparsergroupId>
<artifactId>jsqlparserartifactId>
<version>1.4version>
dependency>
dependencies>
以下环境都可以自己自定义,教程举例User
接下来是代码示例
UserMapper
public interface UserMapper {
//可以自定义相关简单查询
@Select("select * from r_user u,r_order o where o.id = u.uid")
@Results(id = "empMap",value = {
@Result(column = "uid",property = "uid"),
@Result(column = "name",property = "name"),
@Result(column = "password",property = "password"),
@Result(column = "total",property = "order.total"),
@Result(property = "order",
javaType = Order.class,column = "id",
one = @One(select = "cn.marinda.mapper.OrderMapper.findById"))})
List<User> findAllInfo();
UserService
public interface UserService {
List<User> findAllInfo();
}
UserServiceImpl
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
/**
* 添加用户
*/
public List<User> findAllInfo() {
return userMapper.findAllInfo();
}
}
MybatisTest
public class MybatisTest {
public UserMapper userMapper;
@Test
public void tests(){
System.out.println("userMapper:" + userMapper);
List<User> userList = userMapper.findAllInfo();
for(User user : userList){
System.out.println("user: " + user);
}
}
}
结果
user: User{uid='69a1a643-f670-4ef1-b455-a751513b3bb7', name='张三', password='1', sex=男, age=16, portait='http://www.baidu.com', order=Order{id='69a1a643-f670-4ef1-b455-a751513b3bb7', oid='testxx', total=100.0, uid=null}, orderList=null, roleList=null, uidList=null}
user: User{uid='69yt', name='test', password='1', sex=男, age=16, portait='http://www.baidu.com', order=Order{id='69yt', oid='testsss', total=100.0, uid=null}, orderList=null, roleList=null, uidList=null}
错误解析:
由于我的数据源c3p0 版本较低,所以需要更改以下c3p0版本到:0.9.5.2即可!
今天我们讲解了一下SSM学习笔记 ——Spring纯注解整合Mybatis教程
感谢你的观看。