完整塔建一个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 ...
随机推荐
- C# await和Result对比
1.Result 上图是微软官网的截图,由图可知在使用GetXXXX的方法的时候,会阻塞调用其他线程,直到当前异步操作完成,相当于调用wait方法.但是使用异步编程应该避免使用TASK.WAIT或TA ...
- SqlSugar导航查询/多级查询
1.导航查询特点 作用:主要处理主对象里面有子对象这种层级关系查询 1.1 无外键开箱就用 其它ORM导航查询 需要 各种配置或者外键,而SqlSugar则开箱就用,无外键,只需配置特性和主键就能使用 ...
- 强化学习从基础到进阶-常见问题和面试必知必答[3]:表格型方法:Sarsa、Qlearning;蒙特卡洛策略、时序差分等以及Qlearning项目实战
强化学习从基础到进阶-常见问题和面试必知必答[3]:表格型方法:Sarsa.Qlearning:蒙特卡洛策略.时序差分等以及Qlearning项目实战 1.核心词汇 概率函数和奖励函数:概率函数定量地 ...
- 随机 Transformer
在这篇博客中,我们将通过一个端到端的示例来讲解 Transformer 模型中的数学原理.我们的目标是对模型的工作原理有一个良好的理解.为了使内容易于理解,我们会进行大量简化.我们将减少模型的维度,以 ...
- MindSpore导入CUDA算子
技术背景 当今众多的基于Python的AI框架(如MindSpore.PyTorch等)给了开发者非常便利的编程的条件,我们可以用Python的简单的语法写代码,然后由框架在后端自动编译成可以在GPU ...
- Dash 2.15版本新特性介绍
本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/dash-master 大家好我是费老师,Dash不久前发布了其2.15.0版本,新增了一些实用的特性 ...
- Docker从认识到实践再到底层原理(二-1)|容器技术发展史+虚拟化容器概念和简介
前言 那么这里博主先安利一些干货满满的专栏了! 首先是博主的高质量博客的汇总,这个专栏里面的博客,都是博主最最用心写的一部分,干货满满,希望对大家有帮助. 高质量博客汇总 然后就是博主最近最花时间的一 ...
- 洛谷P2241 统计方形 ,棋盘问题升级板,给出格子坐标中矩形以及正方形的计算方法
在做这道题之前我们先了解一下棋盘问题 棋盘问题 (qq.com) 对于棋盘问题,我们可以得出对于一个n*n的正方形方格阵如何求其包含的正方形个数 也就是数每个正方形的中间点,然后将其点排列 ...
- Java注解支持的类型
我们经常会自定义注解,自定义注解时,可能会需要定义各种数据类型,但是自定义注解可以包含哪些数据类型,是存在限制的. 主要有如下几种: A primitive type : 基本类型(java的八种基本 ...
- IDEA 2024.1:Spring支持增强、GitHub Action支持增强、更新HTTP Client等
有段时间没有更新IDEA了,早上看到 IntelliJ IDEA 2024.1 EAP 5发布的邮件提示,瞄了一眼,发现真的是越来越强了,其中不少功能对我来说还是非常有用的.也许这些能力对关注DD的小 ...