mybatis-plus基本入门
1,导包(不需要mybatis的包)
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.0.8</version>
</dependency>
2,配置文件
①
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--加载配置文件-->
<context:property-placeholder location="classpath*:application.properties"/>
<!-- 数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${db.master.url}"/>
<property name="username" value="${db.master.user}"/>
<property name="password" value="${db.master.password}"/>
</bean>
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 自动扫描Mapping.xml文件 -->
<property name="mapperLocations" value="classpath*:/mapper/*.xml"></property>
<!--扫描实体类-->
<property name="typeAliasesPackage" value="com.study.entity"/>
<!--mybatis-plus全局配置-->
<property name="globalConfig" ref="globalConfig"/>
</bean>
<!-- MP 全局配置 -->
<bean id="globalConfig" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
<!--主键为自增长-->
<property name="idType" value="0"/>
<!--下划线转驼峰命名-->
<property name="dbColumnUnderline" value="true"/>
</bean>
<!-- MyBatis 动态实现 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 对Dao 接口动态实现,需要知道接口在哪 -->
<property name="basePackage" value="com.study.dao"/>
</bean>
</beans>
②
db.master.url=jdbc:mysql://127.0.0.1:3306/appmanager? useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&useSSL=false
db.master.user=root
db.master.password=12345678
3,dao层如下
public interface UserMapper extends BaseMapper<User> {
}
4,service如下
public interface UserService extends IService<User> {
}
public class UserServiceImpl extends extends ServiceImpl<UserMapper,User> implements UserService {
}
6,测试如下
public class MyTest {
@Test
public void test1() throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
UserMapper mapper = context.getBean(UserMapper.class);
/* User user=new User("admin","123456");
User user1 = mapper.selectOne(user);
System.out.println(user1);*/
/* Wrapper wrapper= new Wrapper() {
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false;
}
};*/
List<User> users = mapper.selectList(new EntityWrapper<User>());
System.out.println(users);
}
}