准备数据表
create database spring _db character set utf8;
use spring _db;
create table tbl_account( id int primary key auto_increment , name varchar ( 35 ) , money double
) ;
maven导入项目中需要的jar包
< dependencies> < dependency> < groupId> org.spring framework</ groupId> < artifactId> spring -context</ artifactId> < version> 5.2.10.RELEASE</ version> </ dependency> < dependency> < groupId> junit</ groupId> < artifactId> junit</ artifactId> < version> 4.12</ version> < scope> test</ scope> </ dependency> < dependency> < groupId> com.alibaba</ groupId> < artifactId> druid</ artifactId> < version> 1.1.16</ version> </ dependency> < dependency> < groupId> mysql</ groupId> < artifactId> mysql-connector-java</ artifactId> < version> 8.0.20</ version> </ dependency> < dependency> < groupId> org.mybatis </ groupId> < artifactId> mybatis </ artifactId> < version> 3.2.8</ version> </ dependency> < dependency> < groupId> org.spring framework</ 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> </ dependencies>
创建jdbc.properties外部配置文件
jdbc.name=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring _db?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
jdbc.username=root
jdbc.password=123456
创建数据源的配置类JdbcConfig
package com. example. config ; import com. alibaba. druid. pool. DruidDataSource ;
import org. spring framework. beans. factory. annotation. Value ; import javax. sql. DataSource ; public class JdbcConfig { @Value ( "${jdbc.name}" ) 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; }
}
创建MyBatis配置类MyBatisConfig
package com. example. config ; import org. mybatis . spring . SqlSessionFactoryBean ;
import org. mybatis . spring . mapper. MapperScannerConfigurer ;
import org. spring framework. context. annotation. Bean ; import javax. sql. DataSource ; public class MyBatisConfig { @Bean public SqlSessionFactoryBean sqlSessionFactory ( DataSource dataSource) { SqlSessionFactoryBean sb= new SqlSessionFactoryBean ( ) ; sb. setTypeAliasesPackage ( "com.example.domain" ) ; sb. setDataSource ( dataSource) ; return sb; } @Bean public MapperScannerConfigurer mapperScannerConfigurer ( ) { MapperScannerConfigurer msc = new MapperScannerConfigurer ( ) ; msc. setBasePackage ( "com.example.dao" ) ; return msc; }
}
创建Spring的配置类
package com. example. config ; import org. spring framework. context. annotation. ComponentScan ;
import org. spring framework. context. annotation. Configuration ;
import org. spring framework. context. annotation. Import ;
import org. spring framework. context. annotation. PropertySource ;
@Configuration
@ComponentScan ( "com.example" )
@PropertySource ( "classpath:jdbc.properties" )
@Import ( { JdbcConfig . class , MyBatisConfig . class } )
public class SpringConfig {
}
创建实体类POJO层
package com. example. damain ; public class Account { private Integer id; private String name; private Double money;
创建数据接口层dao层
package com. example. dao ; import com. example. domain. 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 into tbl_account(name,money)values(#{name},#{money})" ) void save ( Account account) ; @Delete ( "delete from tbl_account where id = #{id} " ) void delete ( Integer id) ; @Update ( "update tbl_account set name = #{name} , money = #{money} where id = #{id} " ) void update ( Account account) ; @Select ( "select * from tbl_account" ) List < Account > findAll ( ) ; @Select ( "select * from tbl_account where id = #{id} " ) Account findById ( Integer id) ;
}
创建Service接口和实现类
package com. example. service ; import com. example. 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. example. service. impl ; import com. example. dao. AccountDao ;
import com. example. domain. Account ;
import com. example. service. AccountService ;
import org. spring framework. beans. factory. annotation. Autowired ;
import org. spring framework. stereotype. Service ; import java. util. List ; @Service
public class AccountServiceImpl implements AccountService { @Autowired private AccountDao dao; @Override public void save ( Account account) { dao. save ( account) ; } @Override public void delete ( Integer id) { dao. delete ( id) ; } @Override public void update ( Account account) { dao. update ( account) ; } @Override public List < Account > findAll ( ) { return dao. findAll ( ) ; } @Override public Account findById ( Integer id) { return dao. findById ( id) ; }
}
创建运行类
import com. example. config. SpringConfig ;
import com. example. domain. Account ;
import com. example. service. AccountService ;
import org. junit. Test ;
import org. spring framework. context. annotation. AnnotationConfigApplicationContext ;
public class Test01 { @Test public void test01 ( ) { AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext ( SpringConfig . class ) ; AccountService bean = app. getBean ( AccountService . class ) ; Account account = bean. findById ( 1 ) ; System . out. println ( account) ; }
}