下面是工程目录结构
导入依赖pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itcast</groupId>
<artifactId>TeatMaven</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
</project>
配置文件:applicationContext
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--加载druid资源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}?useSSL=false"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="root"/>
</bean>
<!--配置service作为spring的bean,注入dao-->
<bean id="Service" class="com.itcast.service.ServiceImpl">
<property name="accountmapper" ref="accountmapper"/>
</bean>
<!--spring整合mybatis后控制的创建连接用的对象-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.itcast.pojo.Account"/>
</bean>
<!--加载mybatis映射配置的扫描,将其作为spring的bean进行管理-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itcast.mapper"/>
</bean>
</beans>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db1
jdbc.username=root
jdbc.password=root
mapper
package com.itcast.mapper; import com.itcast.pojo.Account; import java.util.List; public interface Accountmapper { public void delete(int id); public Account find(Integer id); public void save(Account account); List<Account> findAll(); public void update(Account account); }
mapper.xml
<?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.itcast.mapper.Accountmapper">
<!--配置根据id查询-->
<select id="find" resultType="com.itcast.pojo.Account" parameterType="int">
select * from account where id = #{id}
</select>
<!--配置查询所有-->
<select id="findAll" resultType="com.itcast.pojo.Account">
select * from account
</select>
<!--配置保存-->
<insert id="save" parameterType="com.itcast.pojo.Account">
insert into account(id,name)values(#{id},#{name})
</insert>
<!--配置删除-->
<delete id="delete" parameterType="int">
delete from account where id = #{id}
</delete>
<!--配置更新-->
<update id="update" parameterType="com.itcast.pojo.Account">
update account set name=#{name},id=#{id} where id=#{id}
</update>
</mapper>
service
package com.itcast.service;
import com.itcast.pojo.Account;
import java.util.List;
public interface Service {
void save(Account account);
void delete(Integer id);
void update(Account account);
List<Account> findAll();
Account findById(Integer id);
}
serviceImpl
package com.itcast.service;
import com.itcast.mapper.Accountmapper;
import com.itcast.pojo.Account;
import java.util.List;
public class ServiceImpl implements Service {
private Accountmapper accountmapper;
public void save(Account account) {
accountmapper.save(account);
}
public void delete(Integer id) {
accountmapper.delete(id);
}
public void update(Account account) {
accountmapper.update(account);
}
public List<Account> findAll() {
return accountmapper.findAll();
}
public Account findById(Integer id) {
return accountmapper.find(id);
}
public void setAccountmapper(Accountmapper accountmapper) {
this.accountmapper = accountmapper;
}
}
controller
package com.itcast.controller;
import com.itcast.pojo.Account;
import com.itcast.service.Service;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class Controllrt {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Service accountService = (Service) ctx.getBean("Service");
List<Account> all = accountService.findAll();
System.out.println(all);
}
}