标签:tostring common dash str depend sele 注解 连接 默认
springboot默认使用hikaricp连接池,如果要使用其他的连接池,如:druid,c3p0等等,需要另行配置。
首先要引入依赖
jdbc的启动器
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
mysql驱动
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
2.配置参数在application.properties文件中配置mysql参数
server.port=8888 logging.level.org.springframework=DEBUG spring.datasource.url=jdbc:mysql:///eesy spring.datasource.username=root spring.datasource.password=root
整合mybatis:
首先引入依赖:
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.0.2</version> </dependency>
第一个是mybatis的启动器
第二个是通用mapper
首先定义实体类
@Table:指定连接的数据库名字,默认是类的首字母小写
@Id:
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id:
@Id 标注用于声明一个实体类的属性映射为数据库的主键列。该属性通常置于属性声明语句之前,可与声明语句同行,也可写在单独行上。
@Id标注也可置于属性的getter方法之前。
@GeneratedValue:
@GeneratedValue 用于标注主键的生成策略,通过strategy 属性指定。默认情况下,JPA 自动选择一个最适合底层数据库的主键生成策略:SqlServer对应identity,MySQL 对应 auto increment。
在javax.persistence.GenerationType中定义了以下几种可供选择的策略:
–IDENTITY:采用数据库ID自增长的方式来自增主键字段,Oracle 不支持这种方式;
–AUTO: JPA自动选择合适的策略,是默认选项;
–SEQUENCE:通过序列产生主键,通过@SequenceGenerator 注解指定序列名,MySql不支持这种方式
–TABLE:通过表产生主键,框架借由表模拟序列产生主键,使用该策略可以使应用更易于数据库移植。
package cn.itcast.user.pojo; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import java.util.Date; @Table(name = "tb_user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String username; private String password; private String name; private Integer age; private Integer sex; private Date birthday; private Date created; private Date updated; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return username; } public void setUserName(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } 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; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Date getCreated() { return created; } public void setCreated(Date created) { this.created = created; } public Date getUpdated() { return updated; } public void setUpdated(Date updated) { this.updated = updated; } @Override public String toString() { return "User{" + "id=" + id + ", userName=‘" + username + ‘\‘‘ + ", password=‘" + password + ‘\‘‘ + ", name=‘" + name + ‘\‘‘ + ", age=" + age + ", sex=" + sex + ", birthday=" + birthday + ", created=" + created + ", updated=" + updated + ‘}‘; } }
2.定义mapper接口:使用@Mapper注解,声明这是个mapper接口,并且把这个 接口放入容器中管理,但是idea中可能会报错 ,因为这个注解他不认识,但是其实已经放入容器中了,继承 tk.mybatis.mapper.common.Mapper<T> 使用通用 Mapper
@Mapper public interface UserMapper extends tk.mybatis.mapper.common.Mapper<User>{ }
3.编写service标红的使用的是通用 Mapper中的数据操作。
@Service public class UserService { //这里报错是因为idea检查系统误报,因为UserMapper使用的是@Mapper注解,idea不认识,其实已经 在容器中了 @Autowired private UserMapper userMapper; public User queryById(int id){ return userMapper.selectByPrimaryKey(id); } }
4.编写controller
@Autowired private UserService userService; @RequestMapping(value = "/findById",method = RequestMethod.GET) @ResponseBody //返回json数据 public User queryById(@RequestParam(name = "id",required = true)int id){ return userService.queryById(id); }
标签:tostring common dash str depend sele 注解 连接 默认
原文地址:https://www.cnblogs.com/bozhengheng/p/12814951.html