mybatis+spring整合开发

写一个mybatis+spring整合开发的小例子

项目源文件展示
在这里插入图片描述

导入mybatis+spring整合的jar包(百度下载)

  1. 创建实体类、方法接口
//创建实体类的时候,创建的属性名称和mysql的表对应

@Component("test")
public interface test {

    public Admin testing(String name);
}
  1. 创建mybatis配置文件mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    
    <typeAliases>    <!-- 进行别名控制 -->
        <package name="dao"/>
    </typeAliases>
    
    <mappers>    <!-- 配置映射文件 -->
        <mapper resource="mybatis/sql.xml"/>
    </mappers>
    
</configuration>
  1. 创建mybatis映射文件sql.xml
<?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="mapper.test">  <!--使用mapper动态开发-->
    
    <select id="testing" parameterType="String" resultType="admin">
        
        select *
        from admin
        where username = #{name}
        
    </select>
</mapper>
  1. 创建spring的配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop.xsd">


    <!-- 外部连接properties文件 -->
    <context:property-placeholder location="classpath:mybatis/database.properties"/>

    <!-- 配置数据库连接池 -->
    <bean id="datasource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
        <property name="driverClass" value="${jdbc.driver}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}"/>
    </bean>
    
    <!-- 配置SqlSession工厂 -->
    <bean name="SqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="datasource"></property>
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
    </bean>
    
    <!-- 配置mapper动态代理 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
         <property name="basePackage" value="mapper"></property>
    </bean>
</beans>
  1. 创建database.properties文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///book_db
jdbc.username=root
jdbc.password=6112783king  //后三个,需要改为自己的数据库信息

配置好上面,就已经将mybatis和spring进行整合了。

  1. 创建testservlet,进行测试
@WebServlet("/testservlet")
public class testservlet extends HttpServlet {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
         WebApplicationContext wa = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        
         test bean = (test) wa.getBean("test");
         Admin admin = bean.testing("a");
         
         System.out.println("密码是:"+admin.getPassword());
         
    }
}

     把项目在Tomcat运行,在浏览器上输入http://localhost:8080/mybatis+spring/testservlet,

     在软件后台可以输出对应用户的密码。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值