Mybatis和Spring的整合
Customer.java
package com.wanson.pojo;
public class Customer {
private Integer id; // 主键id
private String username; // 客户名称
private String jobs; // 职业
private String phone; // 电话
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", username='" + username + '\'' +
", jobs='" + jobs + '\'' +
", phone='" + phone + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getJobs() {
return jobs;
}
public void setJobs(String jobs) {
this.jobs = jobs;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
CustomerDao.java
***************************************************************************************************************************************************************
package com.wanson.dao;
import com.wanson.pojo.Customer;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository("customerDao")
@Mapper
/*
使用Spring自动扫描Mybatis的接口并装配
Spring将指定包中所有被@Mapper注解标注的接口自动装配为Mybatis的映射接口
* */
public interface CustomerDao {
/**
* 接口方法对应SQL映射文件CustomerMapper.xml中的id
*/
public Customer selectCustomerById(Integer id);
public List<Customer> selectAllUser();
public int addCustomer(Customer customer);
public int updateCustomer(Customer customer);
public int deleteCustomer(Integer id);
}
*****************************************************************************************************************************************************************
package com.wanson.controller;
import com.wanson.dao.CustomerDao;
import com.wanson.pojo.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import java.util.List;
/*
在CustomerController类中调用数据访问接口的方法
* */
@Controller("customerController")
public class CustomerController {
@Qualifier("customerDao")
@Autowired
private CustomerDao customerDao;
public void test(){
// 查询一个用户
System.out.println("******************************查询一个用户*************************************");
Customer customer=customerDao.selectCustomerById(2);
System.out.println(customer);
// 添加一个用户
Customer addCustomerItem=new Customer();
addCustomerItem.setId(3);
addCustomerItem.setUsername("李谷一");
addCustomerItem.setJobs("歌手");
addCustomerItem.setPhone("17658789632");
int add=customerDao.addCustomer(addCustomerItem);
System.out.println("添加了"+add+"条记录");
System.out.println("************************************************************************************************");
System.out.println("*********************************查询所有用户**********************************");
// 查询所有用户
List<Customer> list=customerDao.selectAllUser();
for (Customer mycustomer:list) {
System.out.println(mycustomer);
}
}
}
************************************************************************************************************************************************************************
<?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.wanson.pojo" />
</typeAliases>
</configuration>
**************************************************************************************************************************************************************************
<?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属性,该属性值 通常设置为"SQL映射文件名",
指定了唯一的命名空间
-->
<mapper namespace="com.wanson.dao.CustomerDao">
<!--添加一个用户信息,#{name}为com.wanson.pojo.Customer的属性值-->
<insert id="addCustomer">
insert into customer(id,username,jobs,phone)values (#{id},#{username},#{jobs},#{phone})
</insert>
<!--修改一个用户-->
<update id="updateCustomer"></update>
<!--删除一个用户-->
<delete id="deleteCustomer"></delete>
<select id="selectCustomerById"
parameterType="Integer"
resultType="com.wanson.pojo.Customer">
select * from customer where id=#{id}
</select>
<!--查询所有用户信息-->
<select id="selectAllUser"
resultType="com.wanson.pojo.Customer">
select * from customer;
</select>
</mapper>
***********************************************************************************************************************************************************************
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 读取db.properties -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource">
<!--数据库驱动 -->
<property name="driverClassName" value="${jdbc.driver}" />
<!--连接数据库的url -->
<property name="url" value="${jdbc.url}" />
<!--连接数据库的用户名 -->
<property name="username" value="${jdbc.username}" />
<!--连接数据库的密码 -->
<property name="password" value="${jdbc.password}" />
<!--最大连接数 -->
<property name="maxTotal" value="${jdbc.maxTotal}" />
<!--最大空闲连接 -->
<property name="maxIdle" value="${jdbc.maxIdle}" />
<!--初始化连接数 -->
<property name="initialSize" value="${jdbc.initialSize}" />
</bean>
<!-- 事务管理器,依赖于数据源 -->
<bean id="transactionManager" class=
"org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置MyBatis工厂SqlSessionFactory -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源 -->
<property name="dataSource" ref="dataSource" />
<!--指定核MyBatis心配置文件位置 -->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
<!-- 添加mapper文件映射 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 配置mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wanson.dao"/>
</bean>
<!--指定需要扫描的包(包括子包),使注解生效-->
<context:component-scan base-package="com.wanson.dao"/>
<context:component-scan base-package="com.wanson.controller"/>
</beans>
**********************************************************************************************************************************************************************
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=123456
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5
***************************************************************************************************************************************************************************
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
#此处需要根据自己情况人为修改一下即可
log4j.logger.com.dao=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
posted on 2018-11-28 16:29 Indian_Mysore 阅读(198) 评论(0) 编辑 收藏 举报