Spring Boot(五):如何优雅的使用Mybatis

 简单 xml 模式

     Mapper只需要定义接口,系统会自动根据方法名在映射文件中找对应的 Sql。

     1. 添加相关依赖文件


	org.mybatis.spring.boot
	mybatis-spring-boot-starter
	2.0.0


    mysql
	mysql-connector-java
	runtime

     2. application.properties 添加相关配置

## mybatis配置
mybatis.type-aliases-package=com.aqkc.erp.core.domain
mybatis.mapper-locations=classpath:mapper/*.xml
## 数据库连接池配置
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/erp?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456

 3. 在启动类中添加对 mapper 包扫描@MapperScan

@MapperScan("com.aqkc.erp.core.dao")
@EnableTransactionManagement
@SpringBootApplication
public class ERPApplication{

	public static void main(String[] args) {
		SpringApplication.run(ERPApplication.class, args);
	}
	
}

      或者直接在 Mapper 类上面添加注解@Mapper,建议使用上面那种,不然每个 mapper 加个注解也挺麻烦的

4. 开发 Mapper

/**
 * Mapper接口
 * @author reyco
 *
 * @param 
 */
public interface BaseDao {
	
	/**
	 * 获取一条数据
	 * @param id
	 * @return
	 */
	T get(Integer id);
	/**
	 * 获取多条数据
	 * @param t
	 * @return
	 */
	List	list(T t);
	/**
	 * 新增一条数据
	 * @param obj
	 */
	void save(T t);
	/**
	 * 批量新增
	 * @param list
	 */
	void saveList(List list);
	/**
	 * 修改单条数据
	 * @param obj
	 */
	void update(T t);
	/**
	 * 批量修改
	 * @param list
	 */
	void updateList(List list);

	/**
	 * 删除单体数据
	 * @param obj
	 */
	void remove(Integer id);
	
	/**
	 * 批量删除
	 * @param list
	 */
	void deleteList(List list);
	
}

5. 添加 User 的映射文件


	
	

	

	
		insert into `account` values(#{username},#{password})
		
			select @@identity
		
	

	
		insert into `account`(`username`,`password`)
		values
		
			(
				#{item.username},#{item.password}
			)
		
	
	
	
		update `account` set `username`=#{username},`password`=#{password} where id=#{id}
	

	
		delete from `account` where `id`=#{id}
	

	
		delete from `account`
		where `id` in
		
			#{item.id}
		
	



无配置文件注解版

1.application.properties 添加相关配置     

 mybatis.type-aliases-package=com.aqkc.erp.core.domain

2. 在启动类中添加对 mapper 包扫描@MapperScan

@MapperScan("com.aqkc.erp.core.dao")
@EnableTransactionManagement
@SpringBootApplication
public class ERPApplication{

	public static void main(String[] args) {
		SpringApplication.run(ERPApplication.class, args);
	}
	
}

3.开发 Mapper

public interface AccountDao {
	
	@Select("SELECT * FROM account")
	List getAll();
	
	@Select("SELECT * FROM account WHERE id = #{id}")
	AccountgetOne(Long id);

	@Insert("INSERT INTO account(username,password) VALUES(#{username}, #{password})
	void insert(Account account);

	@Update("UPDATE account SET username=#{username},password=#{password} WHERE id =#{id}")
	void update(Account account);

	@Delete("DELETE FROM accountWHERE id =#{id}")
	void delete(Long id);
}

注意,使用#符号和$符号的不同:

SELECT * FROM account WHERE id = #{id}
执行的sql: SELECT * FROM account WHERE id = ?;

SELECT * FROM account WHERE id = ${id}
执行的sql: SELECT * FROM account WHERE id = 1;

     

使用的时候当作普通的类注入进入就可以了          

@Service("accountService")
public class AccountServiceImpl{
	
	@Autowired
	private AccountDao accountDao;
	
	public Account get(Integer id) {
		return accountDao.get(id);
	}
	

}

你可能感兴趣的:(springboot,java,mybatis,mybatis,spring,boot,java)