SpringBoot构建Service层过程中,报userservice 为null空指针异常,完美解决 踩坑

1.百度不知道多久,大部分教程都试了,实在无解。给大伙猜猜坑。仅供参考不喜勿喷

 

2.报的错

 

3.解决办法,代码参考

controller层

UserService接口

最重要的,我一直忽视掉,前两步做完之后,还是会报另一个错误

No qualifying bean of type 'com.fang.bs.service.UserService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

 

UserServiceImpl实现

 

再次运行成功

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 在Springboot连接Mybatis,可以按照以下步骤编写DAOService: 1. 在pom.xml添加Mybatis和Mybatis-Spring的依赖。 ``` <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.x.x</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.x.x</version> </dependency> ``` 2. 配置Mybatis。在application.yml添加如下配置: ``` mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.example.demo.entity ``` mapper-locations指定了mapper文件的位置,type-aliases-package指定了实体类所在的包名。 3. 编写DAO。创建一个接口,使用@Mapper注解标记。在方法使用@Select、@Insert、@Update、@Delete等注解指定SQL语句。 ``` @Mapper public interface UserMapper { @Select("SELECT * FROM user WHERE id = #{id}") User findById(@Param("id") Long id); } ``` 4. 编写Service。创建一个类,使用@Autowired注解注入DAO。在方法调用DAO的方法。 ``` @Service public class UserService { @Autowired private UserMapper userMapper; public User findById(Long id) { return userMapper.findById(id); } } ``` 这样就可以使用Springboot连接Mybatis了。注意要按照规范编写mapper.xml和实体类。 ### 回答2: 在Spring Boot,连接MyBatis的写法主要包括daoservice。 首先,在dao,我们需要编写MyBatis的Mapper接口和对应的xml文件。Mapper接口定义了数据库操作的方法,而Mapper对应的xml文件定义了具体的SQL语句和映射关系。 1. 创建Mapper接口:在dao下创建一个新的包,通常为mapper,然后创建一个与表对应的Mapper接口,例如UserMapper。在接口定义相关的CRUD方法。 ```java public interface UserMapper { User getUserById(int id); void insertUser(User user); void updateUser(User user); void deleteUser(int id); } ``` 2. 创建Mapper的xml配置文件:在resources目录下,创建与Mapper接口同名的xml文件,例如UserMapper.xml。在xml文件,定义具体的SQL语句和结果集映射关系。 ```xml <mapper namespace="com.example.mapper.UserMapper"> <select id="getUserById" parameterType="int" resultType="com.example.entity.User"> SELECT * FROM user WHERE id = #{id} </select> <insert id="insertUser" parameterType="com.example.entity.User"> INSERT INTO user (id, name) VALUES (#{id}, #{name}) </insert> <update id="updateUser" parameterType="com.example.entity.User"> UPDATE user SET name = #{name} WHERE id = #{id} </update> <delete id="deleteUser" parameterType="int"> DELETE FROM user WHERE id = #{id} </delete> </mapper> ``` 接下来是service的写法,service主要负责将dao操作和业务逻辑进行分离,提供具体的服务。 1. 创建Service类:在service下,创建与表对应的Service类,例如UserService。在Service注入对应的Mapper接口,并提供业务逻辑的方法。 ```java @Service public class UserService { @Autowired private UserMapper userMapper; public User getUserById(int id) { return userMapper.getUserById(id); } public void insertUser(User user) { userMapper.insertUser(user); } public void updateUser(User user) { userMapper.updateUser(user); } public void deleteUser(int id) { userMapper.deleteUser(id); } } ``` 至此,我们通过dao的Mapper接口和serviceService类,实现了MyBatis与Spring Boot的连接和使用。在使用时,通过注入Service类的实例,调用相关方法即可完成业务逻辑的操作。 ### 回答3: 在Spring Boot使用MyBatis连接数据库需要按照以下步骤进行操作: 1. 配置数据库连接:在`application.properties`或`application.yml`文件配置数据库连接信息,包括数据库URL、用户名、密码等。 2. 添加MyBatis、数据库驱动和Spring Boot MyBatis Starter依赖:在`pom.xml`文件添加相关依赖。 3. 创建数据表对应的实体类:创建一个Java类,用于映射数据库的表结构。可以使用注解`@Entity`和`@Table`来指定表名和字段与属性的映射关系。 4. 创建Mapper接口:创建一个接口,用于定义数据库操作方法。可以使用注解`@Mapper`或`@Repository`来标识该接口是MyBatis的Mapper。 5. 创建Mapper XML文件:在resources目录下创建与Mapper接口同名的XML文件,配置SQL语句和结果映射。 6. 创建Service接口和实现类:创建一个接口,定义业务逻辑方法,然后创建一个对应的实现类,实现接口的方法。 7. 在Service实现类注入Mapper接口:使用`@Autowired`注解将Mapper接口注入到Service实现类。 8. 在Controller注入Service接口:使用`@Autowired`注解将Service接口注入到Controller。 9. 在Controller定义请求处理方法:根据具体业务需求,编写处理请求的方法。 10. 运行Spring Boot应用程序:使用`@SpringBootApplication`注解标记启动类,并运行该类的`main`方法,启动Spring Boot应用程序。 以上步骤完成后,DAOService和Controller的连接就完成了,在Controller调用Service的方法,在Service调用DAO的方法,最终实现数据库操作和业务逻辑的整合。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值