demo
1.准备一个表 例如:menu表存有menuId,menuName,menuUrl,ztStatic,deleteZt
2.创建java项目
3.导入对应的jar包
4.创建MenuBean实体类 (记得给上对应的get/set方法)
//记得给上对应的get/set方法
private int menuId;
private String menuName;
private String menuUrl;
private int ztStatic;
private int deleteZt;
5.创建一个测试类 Text
int menuId = 1;
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
MenuService ms = (MenuService)ac.getBean("menuService");
MenuBean mb = ms.getMenuServiceById(menuId);
System.out.println(mb);
6.然后创建接口和对应的实现类 MenuService 实现类:MenuServiceImpl
7.在MenuServiceImpl中注入MenuDao接口
/*注入MenuDao*/
MenuDao ud;
public MenuDao getUd() {
return ud;
}
public void setUd(MenuDao ud) {
this.ud = ud;
}
/*查询用户*/
public MenuBean getMenuServiceById(int menuId) {
return ud.getMenuServiceById(menuId);
}
8.创建MenuDao 接口
9.mapper配置文件,写对应的sql语句
<?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="dao.MenuDao">
<select id="getMenuServiceById" parameterType="int" resultType="bean.MenuBean">
select * from menu where menuId = #{menuId}
</select>
</mapper>
10.配置applicationContext.xml核心文件(重要)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
//上面的是头文件
<!-- dataSoureh加载数据源类 -->
<bean id="bds" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 连接的四大参数和一些连接的设置 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/hospital?characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
<!-- 设置数据库连接池的一些信息 -->
<property name="maxActive" value="1000"/>
<property name="maxIdle" value="30"/>
<property name="maxWait" value="1000"/>
</bean>
<!-- sqlSessionFactory 通过sqlSessionFactoryBean这个类 创建一个sqlSessionFactory 可以操作sqlsession configLocation 加载mybatis核心配置文件dataSoureh加载数据源-->
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载mybatis的配置文件,目的是为了加载mapper文件 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
<!-- 数据库的连接参数 -->
<property name="dataSource" ref="bds"></property>
</bean>
<!-- spring和mybatis dao整合 使用的是mapper代理模式
spring如何注入dao对象
class="org.mybatis.spring.mapper.MapperFactoryBean "
MapperFactoryBean 此类要加载
sqlSessionFactory 是mybatis的工厂 获取 sqlsession
mapperInterface对应的接口 可以改成扫描包的形式
-->
<!-- 配置dao对象 -->
<bean id="ud" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
<property name="mapperInterface" value="dao.MenuDao"></property>
</bean>
<!-- 配置service对象 -->
<bean id="menuService" class="service.MenuServiceImpl">
<property name="ud" ref="ud"></property>
</bean>
</beans>
11.配置sqlMapperConfig.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="bean"/>
</typeAliases>
<!-- 加载mapper文件 -->
<mappers>
<mapper resource="dao/MenuDao.xml"/>
</mappers>
</configuration>