完整塔建一个spring 注解版 mybaties 过程可供复制代码
第一步引导包。新建工程maven模块 pom.xml 中导入相对应包
++++++++++++++++++++++++++++++++++++++++++++ 1 ++++++++++++++++++++++++++++++++++++++++++++++
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.20</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
</dependencies>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 ++++++++++++++++++++
新建配置文件 com.it.config包:分别建用于jdbcConfig mybatisConfig SpringConfig
其中jdbConfigs配置包内容如下:由于用到了第三方配置包,properties 文件内等会讲 本包由主包import 导入,
package com.ithm.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.sql.DataSource;
public class jdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource(){
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++4++++++++++++++++++++++++++
MYbatisConfig 内容如下:本包由主包ippor导入,但组件需要加@bean
package com.ithm.config;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
public class mybatisConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
ssfb.setTypeAliasesPackage("com.ithm.domain"); //申明那些包是pojo 的数据类型
package com.ithm.dommin;
public class Account {
private Integer id;
private String name;
private String money;
public Account() {
}
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 String getMoney() {
return money;
}
public void setMoney(String money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money='" + money + '\'' +
'}';
}
}
ssfb.setDataSource(dataSource);
return ssfb;
}
@Bean
//映射扫描
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("com.ithm.dao"); // 申明那些包是代理接口,就是写好数据库查询方法
package com.ithm.dao; import com.ithm.dommin.Account;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update; import java.util.List; public interface AccountDao { @Insert("insert info money (name,money)values(#{name},#{money})")
// @Insert("insert info money (name,money)values(#{name},#{money})")
void save(Account account);
@Delete("delete from money where id = #{id}")
void delect(Integer id);
@Update("update money set name = #{name},money = #{money} where id = #{id}") void update(Account account);
@Select("select * from money")
List<Account> findAll();
//
@Select("select * from money where id =2")
Account findById(Integer id);
}
return msc;
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++5+++++++++++++++++++++++++++++++++++++++
主包内容
package com.ithm.config;
import org.mybatis.spring.batch.MyBatisBatchItemWriter;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
@Configuration //申明这是一个配置类
@ComponentScan("com.ithm") // 扫描那些包有spring的组件容器
@PropertySource("classpath:jdbc.properties") //外部读入propeties
@Import({jdbcConfig.class, mybatisConfig.class}) //加载导入配置包
public class SpringConfig {
}
++++++++++++++++++++++++++++++++++++++++++++++++++6++++++++++++++++++++
resources配置文件下,jdbc.properties内容
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/xsytest
jdbc.username = root
jdbc.password = 142857
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++7+++++++++++++++++++++++++++++
其余就是无关重要的服务层和怎么入口测试了
服务层代码如下:
package com.ithm.server.ipml;
import com.ithm.dao.AccountDao;
import com.ithm.dommin.Account;
import com.ithm.server.AccountServive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AccountServiceimpl implements AccountServive {
@Autowired
private AccountDao accountDao;
public void save(Account account) {
accountDao.save(account);
}
public void update(Account account) {
accountDao.update(account);
}
public void delete(Integer id) {
accountDao.delect(id);
}
public Account findById(Integer id) {
return accountDao.findById(id);
}
public List<Account> finAll() {
return accountDao.findAll();
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=忘了发接口+++++++++++++++++++++++++++++
package com.ithm.server;
import com.ithm.dommin.Account;
import java.util.List;
public interface AccountServive {
void save(Account account);
void delete(Integer id);
void update(Account account);
List<Account> finAll();
Account findById(Integer id);
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++正常测试方法++++++++++++++++++++++
import com.ithm.config.SpringConfig;
import com.ithm.dommin.Account;
import com.ithm.server.AccountServive;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.sql.DataSource;
public class App {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
AccountServive accountServive = ctx.getBean(AccountServive.class);
Account byId = accountServive.findById(2);
System.out.println(byId);
}
}
+++++++++++++++++++++++++++++++++++++++++++++++junit测试方法+++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.ithm.service;
import com.ithm.config.SpringConfig;
import com.ithm.dommin.Account;
import com.ithm.server.AccountServive;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.sql.DataSource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringConfig.class})
public class AccountSer {
@Autowired
private AccountServive accountServive;
@Test
public void testFindByid(){
Account b = accountServive.findById(2);
System.out.println(b);
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++==2022-4月写的案例,删文件之前备份++++++++++++++++++++++++++++++
完整塔建一个spring 注解版 mybaties 过程可供复制代码的更多相关文章
- 将目录下面所有的 .cs 文件合并到一个 code.cs 文件中,写著作权复制代码时的必备良药
将目录下面所有的 .cs 文件合并到一个 code.cs 文件中,写著作权复制代码时的必备良药 @echo off echo 将该目录下所有.cs文件的内容合并到一个 code.cs 文件中! pau ...
- struts2+hibernate+spring注解版框架搭建以及简单测试(方便脑补)
为了之后学习的日子里加深对框架的理解和使用,这里将搭建步奏简单写一下,目的主要是方便以后自己回来脑补: 1:File--->New--->Other--->Maven--->M ...
- spring注解版
第一.spring框架快速入门 1.1什么是spring 框架 Spring 框架是 Java 应用最广的框架,它的成功来源于理念,而不是技术本身,它的理念包括 IoC (Inversion of C ...
- 用intellj 建一个spring mvc 项目DEMO
spring的起初可能经常碰壁,因为网上的资料都是混乱的xml堆成的,混乱难以理解,我这个也是,阿哈哈哈哈! 新建一个Maven->create from archetype->org.j ...
- spring注解配置启动过程
最近看起spring源码,突然想知道没有web.xml的配置,spring是怎么通过一个继承于AbstractAnnotationConfigDispatcherServletInitializer的 ...
- 使用Spring注解来简化ssh框架的代码编写
目的:主要是通过使用Spring注解的方式来简化ssh框架的代码编写. 首先:我们浏览一下原始的applicationContext.xml文件中的部分配置. <bean id="m ...
- Spring 源码(9)Spring Bean的创建过程的前期准备
回顾总结 到目前为止,Spring源码中AbstractApplicationContext#refresh方法的已经解读到第11个方法finishBeanFactoryInitialization, ...
- spring bean的创建过程
spring的核心容器包括:core.beans.context.express language四个模块.所以对于一个简单的spring工程,最基本的就是依赖以下三个jar包即可: <depe ...
- Spring Boot 实战 —— MyBatis(注解版)使用方法
原文链接: Spring Boot 实战 -- MyBatis(注解版)使用方法 简介 MyBatis 官网 是这么介绍它自己的: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过 ...
- 第二个基础框架 — spring — xml版,没用注解 — 更新完毕
1.什么是spring? 老规矩:百度百科一手 这上面说得太多了,我来提炼一下: spring就是一个轻量级的控制反转( IOC ) 和 面向切面编程( AOP ) 的容量框架.总的来说:本质就是对j ...
随机推荐
- tensorflow语法【tf.concat()详解】
相关文章: [一]tensorflow安装.常用python镜像源.tensorflow 深度学习强化学习教学 [二]tensorflow调试报错.tensorflow 深度学习强化学习教学 [三]t ...
- C++ STL 标准模板库(容器总结)算法
C++ 标准模板库STL,是一个使用模板技术实现的通用程序库,该库由容器container,算法algorithm,迭代器iterator,容器和算法之间通过迭代器进行无缝连接,其中所包含的数据结构都 ...
- 重写JSON.stringify与JSON.parse使其支持解析function类型
const JSONStringify = (option) => { return JSON.stringify(option, (key, val) => { // 处理函数丢失问题 ...
- 【算法】【C语言进阶】C语言字符串操作宝藏级别汇总 strtok函数 strstr函数该怎么用?【超详细的使用解释和模拟实现】
[算法][C语言进阶]C语言字符串操作宝藏级别汇总[超详细的使用解释和模拟实现] 作者: @小小Programmer 这是我的主页:@小小Programmer 在食用这篇博客之前,博主在这里介绍一下其 ...
- P9993 [Ynoi Easy Round 2024] TEST_133 题解
题目链接: [Ynoi Easy Round 2024] TEST_133 首先历史最大加,吉司机跑不掉了,维护历史最大加标记以及历史最大,那么根据吉司机标记思想更新操作应该为 \[new \Left ...
- Kafka-如何重设消费者位移(重设OFFSET)
1. 为什么要重设消费者组位移? 我们知道,Kafka 和传统的消息引擎在设计上是有很大区别的,其中一个比较显著的区别就是,Kafka 的消费者读取消息是可以重演的(replayable). 像 Ra ...
- MaxCompute(ODPS)和Hive的区别
Hive概述 架构于Hadoop之上,可以将结构化的HDFS文件映射成一张表,并提供了类似于SQL语法的HQL查询功能. 核心本质:将HQL语句转换成MapReduce任务. Hive的优缺点 优点 ...
- JS 从零手写一个深拷贝(进阶篇)
壹 ❀ 引 在深拷贝与浅拷贝的区别,实现深拷贝的几种方法一文中,我们阐述了深浅拷贝的概念与区别,普及了部分具有迷惑性的浅拷贝api.当然,我们也实现了乞丐版的深拷贝方法,能解决部分拷贝场景,虽然它仍有 ...
- JS leetcode 买卖股票的最佳时机 题解分析,我离职了。
壹 ❀ 引 昨天下班后,还是找经理提出了辞职,没有犹豫的裸辞,今天与人事的对话不小心被后台的同事听到,一下在公司传开了,下午我与同事们多人对线,被他们的消息轰炸....没错,我真的要走了. 因为什么原 ...
- NC235745 拆路
题目链接 题目 题目描述 有 \(n\) 个城镇,城镇之间有 \(m\) 条道路相连,道路可以看成无向边.每一个城镇都有自己的一个繁荣度 \(v_i\) ,一个城镇 \(u\) 受到的影响 \(p\) ...