导包
注意事项
Dao层接口一定是和它相应的sqlMap.xml文件放在同一包下,而pojo需要单列出来; 在Mybatis中设置别名是在它的核心配置文件sqlMapsqlMapConfig,xml中进行,别名配置的路径是pojo类,但是mappers配置的路径是sqlMap.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: 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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd" >
< context: property-placeholder location = " classpath:jdbc.properties" />
< bean id = " dataSource" class = " org.apache.commons.dbcp.BasicDataSource"
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}" />
< property name = " maxActive" value = " 10" />
< property name = " maxIdle" value = " 5" />
</ bean>
< bean id = " sqlSessionFactoryBean" class = " org.mybatis.spring.SqlSessionFactoryBean" >
< property name = " dataSource" ref = " dataSource" />
< property name = " configLocation" value = " classpath:sqlMapConfig.xml" />
</ bean>
< bean class = " org.mybatis.spring.mapper.MapperScannerConfigurer" >
< property name = " basePackage" value = " com.sm.mapper" />
</ bean>
</ beans>
<?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 = " com.sm.pojo" />
</ typeAliases>
< mappers>
< package name = " com.sm.mapper" />
</ mappers>
</ configuration>
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=root
<?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 = " com.sm.mapper.UserDao" >
< update id = " delete" parameterType = " Integer" >
delete from user where user_id = #{aaa}
</ update>
</ mapper>
public interface UserDao {
public Long delete ( Integer i) ;
}
import org. springframework. context. ApplicationContext;
import org. springframework. context. support. ClassPathXmlApplicationContext;
import com. sm. mapper. UserDao;
public class Test {
public static void main ( String[ ] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext ( "applicationContext.xml" ) ;
UserDao userDao = ac. getBean ( UserDao. class ) ;
userDao. delete ( 25 ) ;
}
}