Spring整合MyBatis、全注解开发

小插曲:

连接数据库报错Can’t connect to local MySQL server through socket /tmp/mysql.sock,看了很多解决方法都跟自身情况不符,结果点了一下 系统偏好设置——下面MySQL服务——Initialize DataBase——Use Legacy Password——设置新密码——OK,就好了。。

准备

项目目录:

 pojo类三个属性,空参有参构造,set get方法:

数据库表:

dao层接口4个抽象方法:

service层接口抽象方法同dao层。

service层实现类,声明dao对象,定义set方法用于注入,实现的方法通通调用dao对象的:

mybatis映射配置文件AccountDao.xml:

(注意:namespace与dao接口的全类名一致,select标签的id与dao接口的方法名一致,另外sql语句里的表名tb_account不要写错!)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dd.sm.dao.AccountDao">
    
    <!--配置根据id查询-->
    <select id="findById" resultType="account" parameterType="int">
        select * from tb_account where id = #{id}
    </select>

    <!--配置查询所有-->
    <select id="findAll" resultType="account">
        select * from tb_account
    </select>

    <!--配置保存-->
    <insert id="save" parameterType="account">
        insert into tb_account(name,money)values(#{name},#{money})
    </insert>

    <!--配置删除-->
    <delete id="delete" parameterType="int">
        delete from tb_account where id = #{id}
    </delete>

    <!--配置更新-->
    <update id="update" parameterType="account">
        update tb_account set name=#{name},money=#{money} where id=#{id}
    </update>
        
</mapper>

jdbc配置文件:

(注意url的写法,jdbc:mysql://IP:端口/数据库名!)

spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!--添加命名空间context,添加两个context的url-->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <!--加载配置文件-->
    <context:property-placeholder location="classpath:db.properties"/>

    <!--管理数据源对象-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${db.driver}"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
    </bean>
    
    <!--管理service实现类对象-->
    <bean id="accountService" class="com.dd.sm.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean>

    <!--管理 创建sql会话的对象-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.dd.sm.pojo"/>
    </bean>

    <!--管理 扫描映射配置文件的对象-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.dd.sm.dao"/>
    </bean>
    
</beans>

Servlet中通过spring调用service实现类对象:

运行输出:

spring注解

作用:代替spring配置文件中的<bean>标签。

原始注解:

  

@Component表示将这个资源交给spring管理。

@Controller、@Service、@Repository由@Component衍生而来,用于不同层,有了语义而已。

只用@Autowired是根据数据类型从spring容器里匹配资源,如果同一类型有多个资源,要用资源名称匹配,写两个注解:

或写一个: 

 @Value用法:读取配置文件:

使用注解:(set方法貌似也不需要了)

 

spring配置文件:

配置让spirng扫描注解的路径:

使用了注解的资源,bean就不需要了:

spring新注解

原始注解的局限:

上面的原始注解不能完全替代xml,如非自定义对象dataSource:

新注解:

@Bean用法:

想把某个第三方对象管理到spring容器中,就定义一个类,类里定义一个返回该对象的方法,在方法上加@Bean,如果后面要注入这个资源,就七起个名称@Bean("XXX"),如果只是交给spring管理,就不需要起名。

全注解开发:

将spring配置文件、mybatis映射配置文件的全部配置项用注解替代。

mybatis映射配置文件:

注解代替:

spring配置文件

新建多个配置类:

——SpringDataSourceConfig——

标签:

​​​​​​​

用注解替代:

标签:

 

 

用注解替代:

——SpringMybatisConfig——

标签:

 

用注解替代:

——SpringConfig——

标签:

 

用注解替代:

——另外——

标签:

 

用注解替代:

 

到此为止,实现了全注解开发。

spring整合JUnit

pom文件:

 

新建测试类:

 

右击运行:

 

 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
对于Spring整合MyBatis注解开发,你可以按照以下步骤进行: 1. 首先,在你的项目中引入相应的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> ``` 2. 创建一个MyBatis的配置文件,比如`mybatis-config.xml`,在该文件中配置MyBatis的相关属性和插件。例如,可以配置数据源、事务管理器等。 3. 创建一个数据源配置类,用于配置数据库连接信息和数据源。可以使用Spring Boot的`application.properties`或`application.yml`文件进行配置。 4. 创建Mapper接口,使用MyBatis注解方式定义SQL语句的映射。例如: ```java @Mapper public interface UserMapper { @Select("SELECT * FROM users") List<User> getAllUsers(); @Insert("INSERT INTO users(name, age) VALUES(#{name}, #{age})") void insertUser(User user); } ``` 5. 创建对应的Mapper XML文件(可选),用于更复杂的SQL语句编写。 6. 在Spring Boot的启动类上加上`@MapperScan`注解,指定扫描Mapper接口所在的包。 ```java @SpringBootApplication @MapperScan("com.example.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 7. 使用@Autowired注解将Mapper接口注入到其他组件中使用。 这样,你就可以在Spring Boot项目中使用MyBatis注解方式进行开发了。希望对你有帮助!如果你还有其他问题,可以继续提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值