标签:扫描 cti return wired spec 接口 get property extend
一、环境搭建
1. 先搭好spring和Mybatis的整合的环境
2.导入Mapper的Maven依赖
<dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>4.0.0-beta3</version> </dependency>
3.修改spring配置文件
<!--整合通用Mapper所做的配置修改 原始:org.mybatis.spring.mapper.MapperScannerConfigurer 通用Mapper: tk.mybatis.spring.mapper.MapperScannerConfigurer --> <bean id="scannerConfigurer" class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> <!--扫描的dao的包的路径--> <property name="basePackage" value="com.li.dao"/> </bean>
二、AccountMapper的接口实现
继承Mapper接口,泛型为实体类
public interface AccountMapper extends Mapper<Account> { }
三、在Service中注入就可以使用其中方法
@Service @Transactional public class AccountServiceImpl { @Autowired private AccountMapper accountMapper; public Account getOne(Account account){ return accountMapper.selectOne(account); } }
四、测试
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class TestSpring { @Autowired private AccountServiceImpl accountService; @Test public void test01(){ Account account=new Account(); account.setName("张三"); Account one = accountService.getOne(account); System.out.println(one); } }
注意:在创建实体类时,如果表名和表中属性名与数据库中不一致可使用注解:@table、@Column
/** * @Table :name:指定数据库中表的名称 */ @Table(name = "account") public class account{ /** * @Id: 主键 * @GeneratedValue(strategy = GenerationType.IDENTITY) :主键自增 */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name="cid") private Long cid; private String name; //此注解可以使改属性不映射到数据库表中 @Transient private List<SpecParam> params;
标签:扫描 cti return wired spec 接口 get property extend
原文地址:https://www.cnblogs.com/cqyp/p/12813090.html