springboot和mybatis整合(二)
来源:互联网 发布:大数据定义和概念 编辑:程序博客网 时间:2024/06/05 18:25
上次是springboot的简单入门。显然,这个对于我们意义并不是很大。我们需要的是一个完整的框架。然后就选择和mybatis进行整合。对于springboot和mybatis整合其实还是比较简单的。大致springboot提供了各种starter,比如mybatis提供mybatis-spring-boot-starter。我们只要添加依赖就好了。
1.添加相关依赖。
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
2.配置文件
springboot还是有一个配置文件的。叫application.yml或application.properties。特别强调的是yml文件的语法,想要表达键值对就是: (冒号后面跟空格。)
spring.datasource.url: jdbc:mysql://localhost:3306/jiaxu?useUnicode=true&characterEncoding=utf8
spring.datasource.username: root
spring.datasource.password: jiaxu123
spring.datasource.driver-class-name: com.mysql.jdbc.Driver
mybatis.typeAliasesPackage: com.cjx.firstSpringboot.pojo
mybatis.mapperLocations: classpath:mapper/*.xml
# server.port=8888 访问端口
# server.address= 访问路径
这是mybatis相关的配置。指定mapper.xml地址和实体类
3.配置差不多了。跑个demo看一看
启动类
package com.cjx.firstSpringboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
@MapperScan("com.cjx.firstSpringboot.mapper")
@EnableAutoConfiguration
@ComponentScan("com.cjx.firstSpringboot.*")
@EnableTransactionManagement
public class SampleController {
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
controller
package com.cjx.firstSpringboot.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cjx.firstSpringboot.pojo.User;
import com.cjx.firstSpringboot.service.UserService;
@RequestMapping("/")
@Controller
@EnableAutoConfiguration
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/list/all")
@ResponseBody
public List<User> listAll() {
return userService.selectAll("cjx");
}
@RequestMapping("/hello")
@ResponseBody
public String hello() {
return "hello";
}
}
service
package com.cjx.firstSpringboot.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cjx.firstSpringboot.mapper.UserMapper;
import com.cjx.firstSpringboot.pojo.User;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> selectAll(String name) {
return userMapper.selectAll(name);
}
}
mapper
package com.cjx.firstSpringboot.mapper;
import java.util.List;
import com.cjx.firstSpringboot.pojo.User;
public interface UserMapper {
List<User> selectAll(String name);
}
pojo
package com.cjx.firstSpringboot.pojo;
import java.io.Serializable;
@SuppressWarnings("serial")
public class User implements Serializable {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
mapper.xml
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cjx.firstSpringboot.mapper.UserMapper">
<select id="selectAll" resultType="com.cjx.firstSpringboot.pojo.User">
select * from user;
</select>
</mapper>
当然,sql可以不再xml中写。直接在对于的方法上加注解写sql,比如
@Select("select * from wx_userinfo;") public Map<String,Object> find();
结果出了个异常:Cannot determine embedded database driver class for database type NONE
配置文件符号搞错了
另注:这只是集成mybatis,要用其他的数据连接池需要单独集成
- springboot和mybatis整合(二)
- SpringBoot整合Mybatis(二)
- SpringBoot和Mybatis整合
- springboot和mybatis整合
- 二十一、SpringBoot整合Mybatis、通用mapper和pageHelp
- SpringBoot--整合Mybatis和Redis
- SpringBoot(五):SpringBoot整合MyBatis
- springBoot(四)整合之MyBatis整合
- [SpringBoot]IDEA快速构建SpringBoot应用项目(二)整合Mybatis + Druid
- SpringBoot整合Spring Security和Mybatis验证
- SpringBoot 框架整合 (二) : MyBatis + Mapper + mybatis-generator
- springboot整合redis(二)
- springboot整合vuejs(二)
- SpringBoot学习:SpringBoot整合mybatis
- springboot 整合mybatis
- springboot+mybatis+dubbo整合
- SpringBoot整合MyBatis
- SpringBoot整合Mybatis
- 用户模式与内核模式(2)
- Android camera onPreviewFrame 图片旋转问题
- 文本操作工具类
- linux下编程实现GPS数据获取与解析(2)
- Oracle SQL之lag()和lead()函数使用
- springboot和mybatis整合(二)
- 页面内锚点定位及跳转方法
- Java Number & Math 类
- ueditor使用
- 一个老鸟发的公司内部整理的 Android 学习路线图
- ArcGIS for Android 离线数据空间查询--I查询
- classpath和classpath*的区别
- 倒计时
- 关于jeecg自动生成代码后,进入前端页面后后端报错的问题