一.在SpringBoot中对持久层(就是dao层对数据库进行操作)进行操作的时候有两种方案。一种是JPA,另一种就是mybatics
二.JPA
1.首先要在pom.xml中增加对Mysql和j pa的支持
<!-- mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<!-- jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2.创建实体类
@Entity//表示这是一个实体类
@Table
(name =
"category_")//表明这个实体类对应的表是category_
public
class
Category {
@Id//表明主键
@GeneratedValue
(strategy = GenerationType.IDENTITY) //表明子增长方式
@Column
(name =
"id"
) //表明对应的数据库字段名
private
int
id;
@Column(name =
"name"
)
private
String name;
public
int
getId() {
return
id;
}
public
void
setId(
int
id)
this
.id = id;
}
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
}
3.创建Dao类
//继承JpaRepository,后面的泛型表示Category表示针对的类是Category,主键类型是Integer
JpaRepository这个接口就提供了CURD,和分页等一系列操作,可以直接拿来用就不用进行二次开发了
public
interface
CategoryDAO
extends
JpaRepository<Category,Integer>{
}
4.使用Controller来进行一系列的操作
public
class
CategoryController {
@Autowired
CategoryDAO categoryDAO;
@RequestMapping
(
"/listCategory"
)
public
String listCategory(Model m)
throws
Exception {
List<Category> cs=categoryDAO.findAll();//获取所有数据
m.addAttribute("cs"
, cs);
return
"listCategory"
;
}}
增:sava()
删:delete()
改:save()
查:getone(id) findall()是查询一大堆
JPA的一些高阶用法1:
具体是怎么使用呢:
在DAO层添加这么一句话
User findByNameAndPassword(String name, String password);
这句话==select * from user where name = ? and password = ?
User findByNameLike(String name);
这个话==select * from user where name like = ?
然后就可以直接在Controller层使用了
User user=UserRepository.findbyNameAndPassword(String name,String password);
JPA的一些高阶用法2:
JPA当中的@Query
public interface TypeRepository extends JpaRepository<Type,Long> {
Type findByName(String name);
@Query("select t from Type t")
List<Type> findTop(Pageable pageable);
}
@Query中的sql语句 Type t应该指的就是Type这个类所对应的那个表,里面不是有那个@Entity什么的吗
意思就是找出这个表里面我们所需要的数据
三.mybatics
1.对mybatics使用注解方式进行操作
第一步.必须在application.properties中添加一些参数
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
第二步.在pom.xml中添加对mysql和mybatics的羁绊
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
第三步.创建mapper类
//要使用@Mapper标注表示这是一个mybatics mapper类
//使用@Selete表示这是操作数据库,同时这也是它和使用xml方式的区别,使用xml方式就是我之前用s s m做天猫商城使用 的那种方式,要有一个xml文件来存数据库操作语句
//增删改查依次对应 insert delete update select
@Mapper
public
interface
CategoryMapper {
@Select
(
"select * from category_ "
)
List<Category> findAll();
}
第四步.一般的controller操作
2.使用xml方式进行开发
第一步.必须在application.properties中添加一些参数
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.datasource.url=jdbc:mysql:
//127.0.0.1:3306/how2java?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-
class
-name=com.mysql.jdbc.Driver
//添加这个注释的作用是指明去哪里找x m l文件,同时指定别名
mybatis.mapper-locations=classpath:com/how2java/springboot/mapper/*.xml
mybatis.type-aliases-
package
=com.how2java.springboot.pojo
第二步.在pom.xml中添加对mysql和mybatics的羁绊
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
第三步.写xml文件
<
mapper
namespace
=
"com.how2java.springboot.mapper.CategoryMapper"
>
<
select
id
=
"findAll"
resultType
=
"Category"
>
select * from category_
</
select
>
</
mapper
>
第四步.写mapper类
@Mapper
public
interface
CategoryMapper {
List<Category> findAll();
}