springboot项目操作数据库之XML配置文件实现
配置文件:
- #数据库访问的url地址
- spring.datasource.url=jdbc:mysql://localhost:3306/web
- #数据库驱动类类名
- spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
- #访问数据库-用户名
- spring.datasource.username=root
- #访问数据库-密码
- spring.datasource.password=root
- #mybatis的配置
- mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
- #指定XML映射配置文件的位置,按照规范路径可以忽略不配置
- mybatis.mapper-locations=classpath:mapper/*.xml
复制代码 XML文件
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.zidiu.mapper.UserMapper">
- <select id="findAll" resultType="com.zidiu.pojo.User">
- select * from User
- </select>
- </mapper>
复制代码 Mapper.java
- package com.zidiu.mapper;
-
- import com.zidiu.pojo.User;
- import org.apache.ibatis.annotations.*;
-
- import java.util.List;
-
- @Mapper
- public interface UserMapper {
- /**
- * 查询全部
- */
- // @Select("select * from user")
- List<User> findAll();
-
- /**
- * 根据id删除
- */
- @Delete("delete from user where id = #{id}")
- Integer deleteById(Integer id);
-
- /**
- * 添加用户
- */
- @Insert("insert into user(username,password,name,age) values(#{username},#{password},#{name},#{age})")
- void insert(User user);
-
- /**
- * 根据id更新用户信息
- */
- @Update("update user set username = #{username},password = #{password},name = #{name},age = #{age} where id = #{id}")
- void update(User user);
-
- /**
- * 根据用户名和密码查询用户信息
- */
- @Select("select * from user where username = #{username} and password = #{password}")
- User findByUsernameAndPassword(@Param("username") String username, @Param("password") String password);
- }
复制代码 测试类:
- package com.zidiu;
-
- import com.zidiu.mapper.UserMapper;
- import com.zidiu.pojo.User;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
-
- import java.util.List;
-
- @SpringBootTest
- public class SpringbootMybatisQuickstartApplicationTests {
- @Autowired
- private UserMapper userMapper;
- @Test
- public void testFindAll(){
- List<User> userList = userMapper.findAll();
- userList.forEach(System.out::println);
- }
- @Test
- public void testDeleteById(){
- Integer i = userMapper.deleteById(36);
- System.out.println("影响行数" + i);
- }
-
- @Test
- public void testInsert(){
- User user = new User();
- user.setUsername("admin");
- user.setPassword("123456");
- user.setName("管理员");
- user.setAge(30);
- userMapper.insert(user);
- }
-
- @Test
- public void testUpdate(){
- User user = new User();
- user.setId(6);
- user.setUsername("admin666");
- user.setPassword("123456");
- user.setName("管理员阿祥");
- user.setAge(30);
- userMapper.update(user);
- }
-
- @Test
- public void testFindByUsernameAndPassword(){
- User user = userMapper.findByUsernameAndPassword("admin666", "123456");
- System.out.println(user);
- }
- }
复制代码 Mybatis官方说明:https://mybatis.net.cn/getting-started.html
标红警告,因为存在同名同包,把其他项目删除就好了。
其他项目移除后就正常了。
SpringBoot3+Vue3开发综合实战项目:
JavaWeb(SpringBoot3+vue3)开发+教学管理系统项目实战
|