<!--MySQL--> <dependency> <groupid>mysql</groupid> mysql-connector-java</artifactid> <scope>runtime</scope> </dependency>
<!--web支持--> <dependency> <groupid>org.springframework.boot</groupid> spring-boot-starter-web</artifactid> </dependency>
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url= jdbc:mysql://localhost:3306/springboottest?useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=root
<!--JDBC支持--> <dependency> <groupid>org.springframework.boot</groupid> spring-boot-starter-jdbc</artifactid> </dependency>
@Data
public class Account {
private int id ;
private String name ;
private double money;
}
<dependency> <groupid>org.projectlombok</groupid> lombok</artifactid> </dependency>
public interface IAccountDao {
int add(Account account);
int update(Account account);
int delete(int id);
Account findAccountById(int id);
Account selectAccountById(int id);
List findAccountList();
}</account>
@Repository
public class AccountDaoImpl implements IAccountDao{
@Autowired private JdbcTemplate jdbcTemplate;
@Override
public int add(Account account) {
return jdbcTemplate.update("INSERT INTO account(name,money) VALUES(?,?)",
account.getName(),account.getMoney());
}
@Override
public int update(Account account) {
return jdbcTemplate.update("UPDATE account SET name=?,money=? WHERE id=?",
account.getName(),account.getMoney(),account.getId());
}
@Override
public int delete(int id) {
return jdbcTemplate.update("DELETE FROM TABLE account WHERE id=?", id);
}
@Override
public Account findAccountById(int id) {
List list = jdbcTemplate.query("SELECT * FROM account WHERE id = ?",
new Object[]{id}, new BeanPropertyRowMapper(Account.class));
if(list!=null && list.size()>0){
Account account = list.get(0);
return account;
}else{
return null;
}
}
@Override
public Account selectAccountById(int id){
return jdbcTemplate.queryForObject("SELECT * FROM account WHERE id = ?", new RowMapper() {
@Override
public Account mapRow(ResultSet resultSet, int i) throws SQLException {
Account account = new Account();
account.setId(resultSet.getInt("id"));
account.setName(resultSet.getString("name"));
account.setMoney(resultSet.getDouble("money"));
return account;
}
},id);
}
@Override
public List findAccountList() {
List list = jdbcTemplate.query("SELECT * FROM account",
new Object[]{}, new BeanPropertyRowMapper(Account.class));
if(list!=null && list.size()>0) return list;
else return null;
}
}</account></account></account></account>
public interface IAccountService {
int add(Account account);
int update(Account account);
int delete(int id);
Account findAccountById(int id);
Account selectAccountById(int id);
List findAccountList();
}</account>
@Service
public class JdbcAccountService implements IAccountService {
@Autowired private IAccountDao accountDao;
@Override
public int add(Account account) {
return accountDao.add(account);
}
@Override
public int update(Account account) {
return accountDao.update(account);
}
@Override
public int delete(int id) {
return accountDao.delete(id);
}
@Override
public Account findAccountById(int id) {
return accountDao.findAccountById(id);
}
@Override
public Account selectAccountById(int id) {
return accountDao.selectAccountById(id);
}
@Override
public List findAccountList() {
return accountDao.findAccountList();
}
}
</account>
@RestController
@RequestMapping("/jdbc/account")
public class JdbcAccountController {
@Autowired private IAccountService accountService;
@RequestMapping(value = "/list",method = RequestMethod.<em>GET</em>)
public List getAccounts(){
return accountService.findAccountList();
}
@RequestMapping(value = "/{id}",method = RequestMethod.<em>GET</em>)
public Account getAccountById(@PathVariable("id") int id){
// return accountService.findAccountById(id);
return accountService.selectAccountById(id);
}
@RequestMapping(value = "/{id}",method = RequestMethod.<em>PUT</em>)
public String updateAccount(@PathVariable("id")int id ,
@RequestParam(value = "name",required = true)String name,
@RequestParam(value = "money",required = true)double money){
Account account=new Account();
account.setMoney(money);
account.setName(name);
account.setId(id);
int t=accountService.update(account);
if(t==1){
return account.toString();
}else {
return "fail";
}
}
@RequestMapping(value = ""/*,method = RequestMethod.POST*/)
public String postAccount( @RequestParam(value = "name")String name,
@RequestParam(value = "money")double money){
Account account=new Account();
account.setMoney(money);
account.setName(name);
int t= accountService.add(account);
if(t==1){
return account.toString();
}else {
return "fail";
}
}
}</account>
<!--JPA支持--> <dependency> <groupid>org.springframework.boot</groupid> spring-boot-starter-data-jpa</artifactid> </dependency>
public interface AccountDao extends JpaRepository {
}</account,integer>
@RestController
@RequestMapping("/jpa/account")
public class JpaAccountController {
@Autowired private AccountDao accountDao;
@RequestMapping(value = "/list",method = RequestMethod.<em>GET</em>)
public List getAccounts(){
return accountDao.findAll();
}
@RequestMapping(value = "/{id}",method = RequestMethod.<em>GET</em>)
public Account getAccountById(@PathVariable("id") int id){
return accountDao.findOne(id);
}
@RequestMapping(value = "/{id}",method = RequestMethod.<em>PUT</em>)
public String updateAccount(@PathVariable("id")int id ,
@RequestParam(value = "name",required = true)String name,
@RequestParam(value = "money",required = true)double money){
Account account=new Account();
account.setMoney(money);
account.setName(name);
account.setId(id);
Account account1 = accountDao.saveAndFlush(account);
return account1.toString();
}
@RequestMapping(value = ""/*,method = RequestMethod.POST*/)
public String postAccount( @RequestParam(value = "name")String name,
@RequestParam(value = "money")double money){
Account account=new Account();
account.setMoney(money);
account.setName(name);
Account account1 = accountDao.save(account);
return account1.toString();
}
}</account>
<!--MyBatis支持--> <dependency> <groupid>org.mybatis.spring.boot</groupid> mybatis-spring-boot-starter</artifactid> <version>1.2.0</version> </dependency>
@Mapper
public interface AccountMapper {
@Insert("INSERT INFO account(name,money) VALUES(#{name},#{money})")
int add(@Param("name")String name,
@Param("money")double money);
@Update("UPDATE account SET name = #{name}, money = #{money} WHERE id = #{id}")
int update(@Param("name") String name, @Param("money") double money, @Param("id") int id);
@Delete("DELETE FROM account WHERE id = #{id}")
int delete(int id);
@Select("SELECT id, name AS name, money AS money FROM account WHERE id = #{id}")
Account findAccount(@Param("id") int id);
@Select("SELECT id, name AS name, money AS money FROM account")
List findAccountList();
}
</account>
@Service
public class MybatisAccountService {
@Autowired private AccountMapper accountMapper;
public int add(String name, double money) {
return accountMapper.add(name, money);
}
public int update(String name, double money, int id) {
return accountMapper.update(name, money, id);
}
public int delete(int id) {
return accountMapper.delete(id);
}
public Account findAccount(int id) {
return accountMapper.findAccount(id);
}
public List findAccountList() {
return accountMapper.findAccountList();
}
}</account>
@RestController
@RequestMapping("/mybatis/account")
public class MybatisAccountController {
@Autowired private MybatisAccountService mybatisAccountService;
@RequestMapping(value = "/list", method = RequestMethod.<em>GET</em>)
public List getAccounts() {
return mybatisAccountService.findAccountList();
}
@RequestMapping(value = "/{id}", method = RequestMethod.<em>GET</em>)
public Account getAccountById(@PathVariable("id") int id) {
return mybatisAccountService.findAccount(id);
}
@RequestMapping(value = "/{id}", method = RequestMethod.<em>PUT</em>)
public String updateAccount(@PathVariable("id") int id,
@RequestParam(value = "name", required = true) String name,
@RequestParam(value = "money", required = true) double money) {
int t= mybatisAccountService.update(name,money,id);
if(t==1) {
return "success";
}else {
return "fail";
}
}
@RequestMapping(value = "/{id}", method = RequestMethod.<em>DELETE</em>)
public String delete(@PathVariable(value = "id")int id) {
int t= mybatisAccountService.delete(id);
if(t==1) {
return "success";
}else {
return "fail";
}
}
@RequestMapping(value = "", method = RequestMethod.<em>POST</em>)
public String postAccount(@RequestParam(value = "name") String name,
@RequestParam(value = "money") double money) {
int t= mybatisAccountService.add(name,money);
if(t==1) {
return "success";
}else {
return "fail";
}
}
}</account>
public interface AccountMapper1 {
int update(@Param("money") double money, @Param("id") int id);
}
mybatis.mapper-locations=classpath*:mybatis/*Mapper.xml mybatis.type-aliases-package=com.jinwen.www.MYBATIS.bean
<!--?xml version="1.0" encoding="UTF-8"?-->
<mapper namespace="com.jinwen.www.MYBATIS.Dao.AccountMapper1">
<update id="update">
UPDATE account set money=#{money} WHERE id=#{id}
</update>
</mapper>
@Service
public class MybatisAccountService1 {
@Autowired AccountMapper1 accountMapper1;
@Transactional
public void transfer() throws RuntimeException{
accountMapper1.update(90,1);//用户1减10块 用户2加10块
// int i=1/0;//测试事务回滚
accountMapper1.update(110,2);
}
}
@RestController
@RequestMapping("/mybatis/account")
@MapperScan("com.jinwen.www.MYBATIS.Dao")
public class MybatisAccountController1 {
@Autowired private MybatisAccountService1 accountService;
@RequestMapping(value = "/transfer", method = RequestMethod.<em>GET</em>)
public void transfer(){
accountService.transfer();
}
}
<!--beetlsql支持--> <groupid>com.ibeetl</groupid> beetlsql</artifactid> <version>2.9.5</version>
@SpringBootApplication
public class SpringbootpersistenceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootpersistenceApplication.class, args);
}
//配置包扫描
@Bean(name = "beetlSqlScannerConfigurer")
public BeetlSqlScannerConfigurer getBeetlSqlScannerConfigurer() {
BeetlSqlScannerConfigurer conf = new BeetlSqlScannerConfigurer();
conf.setBasePackage("com.jinwen.www.BeetlSQL.Dao");
conf.setDaoSuffix("Dao");
conf.setSqlManagerFactoryBeanName("sqlManagerFactoryBean");
return conf;
}
@Bean(name = "sqlManagerFactoryBean")
@Primary
public SqlManagerFactoryBean getSqlManagerFactoryBean(@Qualifier("datasource") DataSource datasource) {
SqlManagerFactoryBean factory = new SqlManagerFactoryBean();
BeetlSqlDataSource source = new BeetlSqlDataSource();
source.setMasterSource(datasource);
factory.setCs(source);
factory.setDbStyle(new MySqlStyle());
factory.setInterceptors(new Interceptor[]{new DebugInterceptor()});
factory.setNc(new UnderlinedNameConversion());//开启驼峰
factory.setSqlLoader(new ClasspathLoader("/sql"));//sql文件路径
return factory;
}
//配置数据库
@Bean(name = "datasource")
public DataSource getDataSource() {
return DataSourceBuilder.create().url("jdbc:mysql://localhost:3306/springboottest").username("root").password("root").build();
}
// //开启事务
// @Bean(name = "txManager")
// public DataSourceTransactionManager getDataSourceTransactionManager(@Qualifier("datasource") DataSource datasource) {
// DataSourceTransactionManager dsm = new DataSourceTransactionManager();
// dsm.setDataSource(datasource);
// return dsm;
// }
}
@Data
public class Account {
private int id ;
private String name ;
private double money;
}
@SqlResource("account")
public interface BeetlSQLAccountDao extends BaseMapper {
// @SqlStatement(params = "name")
Account selectAccountByName(String name);
}
</account>
<em>selectAccountByName </em><em>=== </em>*根据name获account select * from account where name= #name#
@RestController
@RequestMapping("/beetlsql/account")
public class BeetlSQLAccountController {
@Autowired private BeetlSQLAccountDao beetlSQLAccountDao;
@RequestMapping(value = "/list",method = RequestMethod.<em>GET</em>)
public List getAccounts(){
return beetlSQLAccountDao.all();
}
@RequestMapping(value = "/{id}",method = RequestMethod.<em>GET</em>)
public Account getAccountById(@PathVariable("id") int id){
return beetlSQLAccountDao.unique(id);
}
@RequestMapping(value = "",method = RequestMethod.<em>GET</em>)
public Account getAccountById(@RequestParam("name") String name){
return beetlSQLAccountDao.selectAccountByName(name);
}
@RequestMapping(value = "/{id}",method = RequestMethod.<em>PUT</em>)
public String updateAccount(@PathVariable("id")int id , @RequestParam(value = "name",required = true)String name,
@RequestParam(value = "money" ,required = true)double money){
Account account=new Account();
account.setMoney(money);
account.setName(name);
account.setId(id);
int t= beetlSQLAccountDao.updateById(account);
if(t==1){
return account.toString();
}else {
return "fail";
}
}
@RequestMapping(value = "",method = RequestMethod.<em>POST</em>)
public String postAccount( @RequestParam(value = "name")String name,
@RequestParam(value = "money" )double money) {
Account account = new Account();
account.setMoney(money);
account.setName(name);
KeyHolder t = beetlSQLAccountDao.insertReturnKey(account);
if (t.getInt() > 0) {
return account.toString();
} else {
return "fail";
}
}
}</account>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有