mybatis和spring整合简单案例

一、创建java工程
二、导入jar包

clipboard.png

三、创建config文件夹用于存放配置文件

clipboard.png

1、mybatis文件夹存放mybatis相关配置文件
2、spring文件夹存放spring相关配置文件
3、db.properties为数据库连接配置文件
4、log4j.properties为日志配置文件

四、在po包中创建实体类

clipboard.png

五、配置applicationContext.xml创建SqlSessionFactoryBean

<!--jdbcProperties-->
<context:property-placeholder location="confilg/db.properties"></context:property-placeholder>

<!--dataSource-->
<bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driver}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

<!--sqlSessionFactory-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
    <!--配置文件-->
    <property name="configLocation" value="config/mybatis/SqlMapConfiger.xml"></property>
    <!---->
    <property name="dataSource" ref="dataSource"></property>
</bean>

六、在mapper文件夹中创建接口和映射文件

clipboard.png

UserMapper.java:


public interface UserMapper {

// 根据id查询用户信息

public User findUserById(int id) throws Exception;

// 添加用户信息

public void insertUser(User user) throws Exception;

// 删除用户信息

public void deleteUserById(int id) throws Exception;

// 根据用户名查询信息

public List<User> findUserByNmae(String name) throws Exception;

}

UserMapper.xml


<mapper namespace="com.ssm.mapper.UserMapper">
    <!--在映射文件中配置很多的sql语句-->
    <!--id为标识符,标识着一个sql语句,成为statement的id,sql语句最后将封装到mappedStatement对象中-->
    <!--paramentType表示参数的类型,resultType代表返回集对应的Java类类型-->
    <!--#{}代表占位符,value代表接受输入的参数,参数名就是value,如果输入参数是简单类型,那参数名可以任意-->
    <!--根据id查询-->
    <select id="findUserById" parameterType="int" resultType="com.ssm.po.User">
        select * from user where id=#{value}
    </select>
    <!--根据用户名称查询-->
    <!--${} 代表拼接字符串 接收到的字符串不加任何修饰的拼接在sql语句中,如果传入的参数是简单类型,则参数名只能是value-->
    <select id="findUserByNmae" parameterType="String" resultType="com.ssm.po.User">
        select * from user where name like '%${value}%'
    </select>
    <!--添加用户-->
    <!--引用类型的话,值为java类的属性名-->
    <insert id="insertUser" parameterType="com.ssm.po.User">
        <selectKey keyProperty="id" resultType="int" order="AFTER">
            select last_insert_id()
        </selectKey>
        insert into user values (#{id},#{name},#{age})
    </insert>
    <!--根据id删除用户-->
    <delete id="deleteUserById" parameterType="java.lang.Integer">
        delete from user where id=#{value}
    </delete>
</mapper>

七、在applicationContext.xml中配置mapper,该bean可根据mapper接口创建实例化对象

<bean class="org.mybatis.spring.mapper.MapperFactoryBean" id="mapperFactoryBean">
    <!--mapperInterface指定接口-->
    <property name="mapperInterface" value="com.ssm.mapper.UserMapper"></property>
    <!--配置SqlSessionFactory-->
    <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>

八、测试

创建测试文件夹(文件夹类型为Tests类型)
创建测试类

clipboard.png

测试类代码 UserMapperTest:

public class UserMapperTest {
    @Test
    public void findUserById() throws Exception {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
        UserMapper userMapper = (UserMapper) applicationContext.getBean("mapperFactoryBean");
        User user = userMapper.findUserById(2);
        System.out.println(user);
    }
}

九、结果

User{id=2, name='yajing', age=18}

评论

暂无评论