spring整合mybatis案例
首先需要sql:sql结构下载。
案例目录结构
1.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</groupId>
<artifactId>java-01</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>package com.limei.domain;
import java.io.Serializable;
public class Account implements Serializable {
private Integer id;
private String name;
private Double money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
2.编写业务层接口与实现类
package com.limei.service;
import com.limei.domain.Account;
import java.util.List;
public interface AccountService {
void save(Account account);
void delete(Integer id);
void update(Account account);
List<Account> findAll();
Account findById(Integer id);
}
package com.limei.service.impl;
import com.limei.dao.AccountDao;
import com.limei.domain.Account;
import com.limei.service.AccountService;
import java.util.List;
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public void save(Account account) {
accountDao.save(account);
}
public void update(Account account){
accountDao.update(account);
}
public void delete(Integer id) {
accountDao.delete(id);
}
public Account findById(Integer id) {
return accountDao.findById(id);
}
public List<Account> findAll() {
return accountDao.findAll();
}
}
3.编写持久层接口与对应的xml文件
package com.limei.dao;
import com.limei.domain.Account;
import java.util.List;
public interface AccountDao {
void save(Account account);
void delete(Integer id);
void update(Account account);
List<Account> findAll();
Account findById(Integer id);
}
<?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.limei.dao.AccountDao">
<!--配置根据id查询-->
<select id="findById" resultType="account" parameterType="int">
select * from account where id = #{id}
</select>
<!--配置查询所有-->
<select id="findAll" resultType="account">
select * from account
</select>
<!--配置保存-->
<insert id="save" parameterType="account">
insert into account(name,money)values(#{name},#{money})
</insert>
<!--配置删除-->
<delete id="delete" parameterType="int">
delete from account where id = #{id}
</delete>
<!--配置更新-->
<update id="update" parameterType="account">
update account set name=#{name},money=#{money} where id=#{id}
</update>
</mapper>4.建立applicationContext.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"
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">
<!--加载perperties配置文件的信息,spring的加载方式-->
<context:property-placeholder location="classpath:*.properties"/>
<!--加载druid资源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--配置service作为spring的bean,注入dao-->
<bean id="accountService" class="com.limei.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!--spring整合mybatis后控制的创建连接用的对象-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.limei.domain"/>
</bean>
<!--加载mybatis映射配置的扫描,将其作为spring的bean进行管理-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.limei.dao"/>
</bean>
</beans>5.jdbc.properties资源
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/lianxi
jdbc.username=root
jdbc.password=1234566.Test类
import com.limei.domain.Account;
import com.limei.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext ("applicationContext.xml");
AccountService accountService = (AccountService) ctx.getBean("accountService");
Account ac = accountService.findById(3);
System.out.println(ac);
// Account account = new Account();
// account.setName("Tom");
// account.setMoney(123456.78);
//
// accountService.save(account);
}
}
《神武天尊》国产动漫高清在线免费观看:https://www.jgz518.com/xingkong/11801.html
《酒店实习生第一季》国产剧高清在线免费观看:https://www.jgz518.com/xingkong/87189.html
《神探二又二分之一》剧情片高清在线免费观看:https://www.jgz518.com/xingkong/48601.html
看到你的文章,我仿佛感受到了生活中的美好。 https://www.yonboz.com/video/29701.html
你的文章总是能给我带来欢乐,谢谢你! https://www.yonboz.com/video/60669.html
看到你的文章,我仿佛感受到了生活中的美好。 https://www.yonboz.com/video/29701.html
你的文章内容非常卖力,让人点赞。 https://www.4006400989.com/qyvideo/95652.html
《我要结婚粤语》喜剧片高清在线免费观看:https://www.jgz518.com/xingkong/50656.html
《大叔的哈雷日记》喜剧片高清在线免费观看:https://www.jgz518.com/xingkong/69036.html
你的才华让人惊叹,你是我的榜样。 https://www.yonboz.com/video/43690.html
《帝国2024》喜剧片高清在线免费观看:https://www.jgz518.com/xingkong/127851.html
看到你的文章,我仿佛感受到了生活中的美好。 https://www.4006400989.com/qyvideo/79429.html
你的文章让我感受到了正能量,非常棒! http://www.55baobei.com/xl18hRPioz.html
《我要结婚粤语》喜剧片高清在线免费观看:https://www.jgz518.com/xingkong/50656.html
《大叔的哈雷日记》喜剧片高清在线免费观看:https://www.jgz518.com/xingkong/69036.html
你的文章让我感受到了快乐,每天都要来看一看。 http://www.55baobei.com/6l458h2PAw.html
《帝国2024》喜剧片高清在线免费观看:https://www.jgz518.com/xingkong/127851.html
你的才华横溢,让人敬佩。 https://www.4006400989.com/qyvideo/88235.html
你的文章让我感受到了无尽的欢乐,谢谢分享。 http://www.55baobei.com/GutrO2PSYG.html
《狂迷制造者:拉斯冯提尔》记录片高清在线免费观看:https://www.jgz518.com/xingkong/29268.html
你的文章让我感受到了无尽的欢乐,谢谢分享。 http://www.55baobei.com/GutrO2PSYG.html
《抓住彩虹》泰国剧高清在线免费观看:https://www.jgz518.com/xingkong/154250.html
《Santa Mi Amor》剧情片高清在线免费观看:https://www.jgz518.com/xingkong/26995.html
逆境中的反思充满生命韧性。
?学术类评语?
文章结构紧凑,层次分明,逻辑严密,让人一读即懂。
每一个段落都紧密相连,逻辑清晰,展现了作者高超的写作技巧。
案例丰富,数据详实,论证扎实可信。
独特的构思和新颖的观点,让这篇文章在众多作品中脱颖而出。
案例丰富且贴合主题,论证逻辑环环相扣。
这篇文章不错!