Spring + Mybatis + Mybatis-Plus

  • Mybatis+Mybatis-Plus

引入Spring框架,数据源、构建等工作就交给了Spring管理

pom.xml


	.version>5.2.6.RELEASE</spring.version>
</properties>
	

	
		com.baomidou</groupId>
		mybatis-plus</artifactId>
		3.1.1</version>
	</dependency>
	
		mysql</groupId>
		mysql-connector-java</artifactId>
		5.1.47</version>
	</dependency>
	
		com.alibaba</groupId>
		druid</artifactId>
		1.0.11</version>
	</dependency>
	
		org.projectlombok</groupId>
		lombok</artifactId>
		true</optional>
		1.18.4</version>
	</dependency>
	
		junit</groupId>
		junit</artifactId>
		4.12</version>
	</dependency>
	
		org.slf4j</groupId>
		slf4j-log4j12</artifactId>
		1.6.4</version>
	</dependency>
	
	
		org.springframework</groupId>
		spring-webmvc</artifactId>
		${spring.version}</version>
	</dependency>
	
		org.springframework</groupId>
		spring-jdbc</artifactId>
		${spring.version}</version>
	</dependency>
	
		org.springframework</groupId>
		spring-test</artifactId>
		${spring.version}</version>
	</dependency>
</dependencies>

log4j.properties

log4j.rootLogger=DEBUG,A1

log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%t] [%c]-[%p] %m%n

实现查询User

第一步,编写jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false
jdbc.username=root
jdbc.password=123456

第二步,编写applicationContext.xml

"1.0" encoding="UTF-8"?>
"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">

	-placeholder
		location="classpath:*.properties" />

	<!-- 定义数据源 -->
	"dataSource"
		class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
		"url" value="${jdbc.url}" />
		"username" value="${jdbc.username}" />
		"password" value="${jdbc.password}" />
		"driverClassName" value="${jdbc.driver}" />
		"maxActive" value="10" />
		"minIdle" value="5" />
	</bean>

	<!--这里使用Mybatis-Plus提供的sqlSessionFactory,完成了Spring与Mybatis-Plus的整合 -->
	"sqlSessionFactory"
		class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
		"dataSource" ref="dataSource" />
		"configLocation"
			value="classpath:mybatis-config.xml" />
		"globalConfig">
			class="com.baomidou.mybatisplus.core.config.GlobalConfig">
				"dbConfig">
					class="com.baomidou.mybatisplus.core.config.GlobalConfig$DbConfig">
						"idType" value="AUTO" />
					</bean>
				</property>
			</bean>
		</property>
	</bean>

	<!--扫描mapper接口,使用的依然是Mybatis原生的扫描器 -->
	class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		"basePackage" value="cn.com.javakf.mapper" />
		</bean>
	
	</beans>

第三步,编写mybatis-config.xml

"1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
      PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
      "http://mybatis.org/dtd/mybatis-3-config.dtd">


</configuration>

第四步,编写User对象以及UserMapper接口

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("tb_user")
public class User {

	private Long id;
	private String userName;
	private String password;
	private String name;
	private Integer age;
	private String email;

}
public interface UserMapper extends BaseMapper<User> {

}

第五步,编写测试用例

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class TestMybatisSpring {

	@Autowired
	private UserMapper userMapper;

	@Test
	public void testSelectList() {
		List<User> users = this.userMapper.selectList(null);
		for (User user : users) {
			System.out.println(user);
		}
	}

}

测试结果

[main] [cn.com.javakf.mapper.UserMapper.selectList]-[DEBUG] ==>  Preparing: SELECT id,user_name,password,name,age,email FROM tb_user 
[main] [cn.com.javakf.mapper.UserMapper.selectList]-[DEBUG] ==> Parameters: 
[main] [cn.com.javakf.mapper.UserMapper.selectList]-[DEBUG] <==      Total: 5
[main] [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@10567255]
User(id=1, userName=zhangsan, password=123456, name=张三, age=18, email=zhangsan@163.com)
User(id=2, userName=lisi, password=123456, name=李四, age=20, email=lisi@163.com)
User(id=3, userName=wangwu, password=123456, name=王五, age=28, email=wangwu@163.com)
User(id=4, userName=zhaoliu, password=123456, name=赵六, age=21, email=zhaoliu@163.com)
User(id=5, userName=sunqi, password=123456, name=孙七, age=24, email=sunqi@163.com)

代码托管:spring-mybatis-mybatis-plus

你可能感兴趣的:(Mybatis-Plus)