下面是详细讲解“详解Spring与Mybatis的整合方法(基于Eclipse的搭建)”的完整攻略。
首先要确保Eclipse中已经安装好Spring和Mybatis的插件(可以在“Marketplace”中搜索安装)。然后需要准备好数据库和相关的配置文件。
新建Maven项目,选择需要的包和依赖。
新建数据库,在Mybatis的配置文件中配置数据库连接信息。
示例:
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="UNPOOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
</configuration>
示例:
Mybatis映射文件
<mapper namespace="com.example.dao.UserDao">
<select id="getUserById" resultType="com.example.pojo.User">
select * from user where id=#{id}
</select>
</mapper>
Mybatis接口
public interface UserDao {
User getUserById(@Param("id") String id);
}
Spring配置文件
<!-- Mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.example.dao"/>
</bean>
示例:
<!-- 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
<property name="user" value="root"/>
<property name="password" value=""/>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
示例:
<!-- 注解扫描 -->
<context:component-scan base-package="com.example"/>
<!-- MVC组件扫描 -->
<mvc:annotation-driven/>
<context:component-scan base-package="com.example.controller"/>
下面给出两个基于Spring和Mybatis整合的例子。
public class User {
private int id;
private String name;
private int age;
// 省略getter和setter方法
}
public interface UserDao {
User getUserById(@Param("id") String id);
}
<mapper namespace="com.example.dao.UserDao">
<select id="getUserById" resultType="com.example.pojo.User">
select * from user where id=#{id}
</select>
</mapper>
<!-- Mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.example.dao"/>
</bean>
<!-- 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
<property name="user" value="root"/>
<property name="password" value=""/>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 注解扫描 -->
<context:component-scan base-package="com.example"/>
<!-- MVC组件扫描 -->
<mvc:annotation-driven/>
<context:component-scan base-package="com.example.controller"/>
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserDao userDao;
@RequestMapping("/getUserById")
@ResponseBody
public User getUserById(String id) {
return userDao.getUserById(id);
}
}
新建MySQL数据表user(数据表结构为id, name, age)。
新建Java类User(位于com.example.pojo包下)。
public class User {
private int id;
private String name;
private int age;
// 省略getter和setter方法
}
public interface UserDao {
void addUser(User user);
}
<mapper namespace="com.example.dao.UserDao">
<insert id="addUser" parameterType="com.example.pojo.User">
insert into user(name, age) values(#{name}, #{age})
</insert>
</mapper>
<!-- Mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.example.dao"/>
</bean>
<!-- 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
<property name="user" value="root"/>
<property name="password" value=""/>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 注解扫描 -->
<context:component-scan base-package="com.example"/>
<!-- MVC组件扫描 -->
<mvc:annotation-driven/>
<context:component-scan base-package="com.example.controller"/>
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserDao userDao;
@RequestMapping("/addUser")
@ResponseBody
public String addUser(User user) {
userDao.addUser(user);
return "添加成功!";
}
}
以上就是详解Spring与Mybatis的整合方法(基于Eclipse的搭建)的完整攻略。在整个过程中,需要注意配置文件的路径和对应关系,尤其是在Maven项目中。同时,可以通过实现一些简单的示例来快速掌握整合方法的实现。
本文链接:http://task.lmcjl.com/news/8111.html