gpt4 book ai didi

spring - 如何用 spring 启动 myBatis 实现?

转载 作者:行者123 更新时间:2023-12-04 22:35:00 25 4
gpt4 key购买 nike

我想和 myBatis 一起工作。我已经阅读了 MyBatis-3-user-guide。现在我正在尝试实现它。最近我学习了spring。所以我很难实现它。所以我需要一些有用的资源,我可以通过它一步一步地实现它。

最佳答案

首先在你的类路径中添加 MyBatis-Spring jar

从你的 spring 上下文文件开始。在你的上下文文件中添加以下几行

<beans:bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="yourDriverClassName"
p:url="yourUrl"
p:username="yourUsername"
p:password="yourPassword" />

<beans:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="configLocation" value="/WEB-INF/mybatis-config.xml" />
</beans:bean>
<beans:bean id="userDao" class="com.yourcomp.dao.Userdao">
<beans:property name="sqlSessionFactory" ref="sqlSessionFactory" />
</beans:bean>

你的 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>
<settings>

</settings>
<typeAliases>

<typeAlias alias="User" type ="com.yourcomp.domain.User" />

</typeAliases>
<mappers>

<mapper resource="com/yourcomp/domain/UserMapper.xml"/>
<mapper resource="com/yourcomp/domain/AnotherDomainObjectMapper.xml"/>
</mappers>

</configuration>

你在 src/com/yourcomp/domain/中的 userMapper.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="org.pbl.rms.RMSUserDao">-->

<mapper namespace="com.yourcomp.domain.User">

<resultMap id="userMap" type="User">
<id property="userId" column="USER_ID" javaType="int" jdbcType="NUMERIC"/>
<result property="userName" column="USER_NAME" javaType="String" jdbcType="VARCHAR"/>
<result property="userFullName" column="USER_FULL_NAME" javaType="String" jdbcType="VARCHAR"/>
<result property="password" column="PASSWORD" javaType="String" jdbcType="VARCHAR"/>
<result property="passwordExpiryDate" column="PASWRD_EXPIRY_DATE" javaType="java.util.Date" jdbcType="DATE"/>
<result property="status" column="STATUS" javaType="_integer" jdbcType="DECIMAL"/>
</resultMap>

<select id="getUserById" parameterType="map" resultMap="userMap">
select * from user where USER_ID=#{userId}
</select>

</mapper>

现在在你的 DAO 层你可能有如下类:
public class UserDAO{

private SqlSessionFactory sqlSessionFactory;

public UserDAO() {

}

public UserDAO(SqlSessionFactory sqlSessionFactory ) {
this.sqlSessionFactory = sqlSessionFactory;

}
public String getUserById(Integer userId) {
SqlSession session = sqlSessionFactory.openSession();

String name=null;
try {
name = (String)session.selectOne("com.yourcomp.domain.User.getUserById",userId);
}catch(Exception e){

}finally {
session.close();
}
return name;
}
}

关于spring - 如何用 spring 启动 myBatis 实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8999041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com