public class MyUser {
private Integer uid; //主键
private String uname;
private String usex;
//setter和getter方法
@Override
public String toString() {
return "MyUser{" +
"uid=" + uid +
", uname='" + uname + '\'' +
", usex='" + usex + '\'' +
'}';
}
}
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.dao.UserDao">
<select id="selectUserById" parameterType="Integer"
resultType="com.mybatis.po.MyUser">
select * from user where uid=#{uid}
select>
<select id="selectAllUser" resultType="com.mybatis.po.MyUser">
select * from user
select>
<insert id="addUser" parameterType="com.mybatis.po.MyUser">
insert into user (uname,usex) values (#{uname},#{usex})
insert>
<update id="updateUser" parameterType="com.mybatis.po.MyUser">
update user set uname=#{uname},usex=#{usex} where uid=#{uid}
update>
<delete id="deleteUser" parameterType="Integer">
delete from user where uid=#{uid}
delete>
mapper>
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<mappers>
<mapper resource="com/mybatis/mapper/UserMapper.xml"/>
mappers>
configuration>
@Repository("userDao")
@Mapper
/**
* 使用Spring自动扫描MyBatis的接口并进行装配
*(Spring中将指定包中所有被@Mapper注解的接口自动装配为MyBatis映射接口)
*/
public interface UserDao {
//接口方法对应映射文件UserMapper.xml中的id
MyUser selectUserById(Integer uid);
List<MyUser> selectAllUser();
int addUser(MyUser myUser);
int updateUser(MyUser myUser);
int deleteUser(Integer uid);
}
@Controller("userController")
public class UserController {
@Autowired
private UserDao userDao;
public void test(){
MyUser auser=userDao.selectUserById(1);
System.out.println(auser);
System.out.println("============");
//添加一个用户
MyUser addmu=new MyUser();
addmu.setUname("小强");
addmu.setUsex("男");
int add=userDao.addUser(addmu);
System.out.println("添加了"+add+"条记录!");
System.out.println("============");
//修改一个用户
MyUser updatemu=new MyUser();
updatemu.setUid(1);
updatemu.setUname("王五");
updatemu.setUsex("男");
int up=userDao.updateUser(updatemu);
System.out.println("修改了"+up+"条纪录");
System.out.println("============");
//删除一个用户
int dl=userDao.deleteUser(9);
System.out.println("删除了"+dl+"条记录");
System.out.println("============");
//查询所有用户
List<MyUser> list=userDao.selectAllUser();
for(MyUser myUser:list){
System.out.println(myUser);
}
}
}
<?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: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">
<!--指定需要扫描的包(包括子包),使注解生效-->
<context:component-scan base-package="com.mybatis.dao"/>
<context:component-scan base-package="com.mybatis.controller"/>
<!--配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/springtest?characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
<!--最大连接数-->
<property name="maxTotal" value="30"/>
<!--最大空闲数-->
<property name="maxIdle" value="10"/>
<!--初始化连接数-->
<property name="initialSize" value="5"/>
</bean>
<!--添加事务支持-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启事务注解-->
<tx:annotation-driven transaction-manager="txManager"/>
<!--配置MyBatis工厂,同时指定数据源,并于MyBatis完美整合-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:com/mybatis/mybatis-config.xml"/>
</bean>
<!--Mapper代理开发,使用Spring自动扫描MyBatis的接口并装配
(Spring指定包中所有被@Mapper注解标注的接口自动装配为Mybatis的映射接口)
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--mybatis-spring组件扫描器-->
<property name="basePackage" value="com.mybatis.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
public class TestController {
public static void main(String[] args) {
ApplicationContext appCon=new ClassPathXmlApplicationContext("application.xml");
UserController uc= (UserController) appCon.getBean("userController");
uc.test();
}
}