mysql xml 注释_Spring Boot + Mybatis 全注解与XML

IDEA 创建项目

103565a3e579

创建项目.PNG

全注解方式

Mybatis maper的全注解形式,不用dao接口,不用mapper的xml文件,都在一个类里面

/**

* mybatis的dao接口和mapper配置文件的合体

*/

@Mapper

@Repository

public interface CustomerMapper {

@Select("SELECT * FROM customers")

List findAll();

}

application.properties

server.port=8080

server.servlet.context-path=/boot

spring.mvc.view.prefix=/WEB-INF/views/

spring.mvc.view.suffix=.jsp

# mysql

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.url=jdbc:mysql://localhost:3306/shpun?useUnicode=true&characterEncoding=UTF-8

spring.datasource.username=root

spring.datasource.password=root

测试通过

@RunWith(SpringRunner.class)

@SpringBootTest

public class TestspringbootApplicationTests {

@Autowired

private CustomerMapper customerMapper;

@Test

public void testCustomerMapper(){

System.out.println(customerMapper.findAll());

}

}

xml方式

mybatis-config.xml mybatis的配置文件

/p>

"http://mybatis.org/dtd/mybatis-3-config.dtd">

CustomerMapper.xml mapper的xml文件

/p>

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select * from customers

application.properties

server.port=8888

server.servlet.context-path=/yml

spring.mvc.view.prefix=/WEB-INF/views/

spring.mvc.view.suffix=.jsp

# mysql

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.url=jdbc:mysql://localhost:3306/shpun?useUnicode=true&characterEncoding=UTF-8

spring.datasource.username=root

spring.datasource.password=root

# mybatis

mybatis.type-aliases-package=com.shpun.bean

mybatis.config-location=classpath:mybatis/mybatis-config.xml

mybatis.mapper-locations=classpath:mapper/*.xml

dao 接口

@Mapper

@Repository

public interface CustomerDao{

List selectAllCustomers();

}

测试通过

@RunWith(SpringRunner.class)

@SpringBootTest

public class TestspringbootApplicationTests {

@Autowired

private CustomerDao customerDao;

@Test

public void testCustomerDao(){

System.out.println(customerDao.selectAllCustomers());

}

}

ps:

先前用xml的时候,提示错误找不到dao的bean,在dao上添加

@Mapper

如果是多个dao接口的话,每个都加比较麻烦,可以在SpringBootApplication的java类中添加

@MapperScan("com.shpun.dao")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值